Implement Different Types of AlertBox using Alerter Library in Android
Last Updated :
06 Jun, 2021
In this article, we are going to implement a very important feature related to the AlertBox. Usually, we create an AlertBox to show some important content. Here we are going to learn how to implement that feature in our Android App using Alert Library. A sample video is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language.
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Add dependency and JitPack Repository
Navigate to the Gradle Scripts > build.gradle(Module:app) and add the below dependency in the dependencies section.
implementation 'com.tapadoo.android:alerter:2.0.4'
Add the JitPack repository to your build file. Add it to your root build.gradle at the end of repositories inside the allprojects{ } section.
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
After adding this dependency sync your project and now we will move towards its implementation.
Step 3: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:keepScreenOn="true"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/txt1"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_margin="10dp"
android:background="#fff"
android:padding="8dp"
android:text="Simple Alert "
android:textColor="#000000"
android:textSize="24dp"
android:textStyle="bold" />
<TextView
android:id="@+id/txt2"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_margin="10dp"
android:background="#fff"
android:padding="8dp"
android:text="with Background Color "
android:textColor="#000000"
android:textSize="24dp"
android:textStyle="bold" />
<TextView
android:id="@+id/txt3"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_margin="10dp"
android:background="#fff"
android:padding="8dp"
android:text="with Icon"
android:textColor="#000000"
android:textSize="24dp"
android:textStyle="bold" />
<TextView
android:id="@+id/txt4"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_margin="10dp"
android:background="#fff"
android:padding="8dp"
android:text="with On Screen Duration "
android:textColor="#000000"
android:textSize="24dp"
android:textStyle="bold" />
<TextView
android:id="@+id/txt5"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_margin="10dp"
android:background="#fff"
android:padding="8dp"
android:text="without Title "
android:textColor="#000000"
android:textSize="24dp"
android:textStyle="bold" />
<TextView
android:id="@+id/txt6"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_margin="10dp"
android:background="#fff"
android:padding="8dp"
android:text="with OnClickListener "
android:textColor="#000000"
android:textSize="24dp"
android:textStyle="bold" />
<TextView
android:id="@+id/txt7"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_margin="10dp"
android:background="#fff"
android:padding="8dp"
android:text="with Verbose Text"
android:textColor="#000000"
android:textSize="24dp"
android:textStyle="bold" />
<TextView
android:id="@+id/txt8"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_margin="10dp"
android:background="#fff"
android:padding="8dp"
android:text="with Swipe To Dismiss"
android:textColor="#000000"
android:textSize="24dp"
android:textStyle="bold" />
<TextView
android:id="@+id/txt9"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_margin="10dp"
android:background="#fff"
android:padding="8dp"
android:text="with Progress Bar"
android:textColor="#000000"
android:textSize="24dp"
android:textStyle="bold" />
<TextView
android:id="@+id/txt10"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_margin="10dp"
android:background="#fff"
android:padding="8dp"
android:text="with Visibility Callbacks"
android:textColor="#000000"
android:textSize="24dp"
android:textStyle="bold" />
</LinearLayout>
Step 4: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Java
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.tapadoo.alerter.Alerter;
import com.tapadoo.alerter.OnHideAlertListener;
import com.tapadoo.alerter.OnShowAlertListener;
public class MainActivity extends AppCompatActivity {
TextView txt1, txt2, txt3, txt4, txt5, txt6, txt7, txt8, txt9, txt10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
txt1 = findViewById(R.id.txt1);
txt2 = findViewById(R.id.txt2);
txt3 = findViewById(R.id.txt3);
txt4 = findViewById(R.id.txt4);
txt5 = findViewById(R.id.txt5);
txt6 = findViewById(R.id.txt6);
txt7 = findViewById(R.id.txt7);
txt8 = findViewById(R.id.txt8);
txt9 = findViewById(R.id.txt9);
txt10 = findViewById(R.id.txt10);
// click on text to show alert
txt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
simpleAlert();
}
});
// click on text to show alert
txt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
withBackgroundColor();
}
});
// click on text to show alert
txt3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
withIcon();
}
});
// click on text to show alert
txt4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
withOnScreenDuration();
}
});
// click on text to show alert
txt5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
withoutTitle();
}
});
// click on text to show alert
txt6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
withOnClickListener();
}
});
// click on text to show alert
txt7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
withVerboseText();
}
});
// click on text to show alert
txt8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
withSwipeToDismiss();
}
});
// click on text to show alert
txt9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
withProgressBar();
}
});
// click on text to show alert
txt10.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
withVisibilityCallbacks();
}
});
}
// show simple alert
public void simpleAlert() {
Alerter.create(MainActivity.this).setTitle("Geeks For Geeks")
.setText("A portal for Computer Science Student").show();
}
// set background color using setBackgroundColorRes
public void withBackgroundColor() {
Alerter.create(this).setTitle("Geeks For Geeks")
// or setBackgroundColorInt(Color.CYAN)
.setText("A portal for Computer Science Student").setBackgroundColorRes(R.color.purple_200)
.show();
}
// without title
public void withoutTitle() {
Alerter.create(this)
.setText("A portal for Computer Science Student").show();
}
public void withOnClickListener() {
Alerter.create(this).setTitle("Geeks For Geeks")
.setText("A portal for Computer Science Student").setDuration(10000).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// showing simple toast to user when clicked
Toast.makeText(MainActivity.this, "You Clicked Me", Toast.LENGTH_LONG).show();
}
}).show();
}
public void withVerboseText() {
Alerter.create(this).setTitle("Geeks For Geeks")
.setText("A portal for Computer Science Student"
+ "A portal for Computer Science Student" + "A portal for Computer Science Student").show();
}
// set icon using setIcon
public void withIcon() {
Alerter.create(this).setTitle("Geeks For Geeks")
// Optional - Removes white tint
.setText("A portal for Computer Science Student").setIcon(R.drawable.ic_launcher_foreground).setIconColorFilter(0)
.show();
}
// set screen duration using setDuration
public void withOnScreenDuration() {
Alerter.create(this).setTitle("Geeks For Geeks")
// set the duration in milli seconds
.setText("A portal for Computer Science Student").setDuration(10000)
.show();
}
// swipe toast to dismiss
public void withSwipeToDismiss() {
Alerter.create(this).setTitle("Geeks For Geeks")
// enabled swipe to dismiss
.setText("A portal for Computer Science Student").enableSwipeToDismiss()
.show();
}
// show progress bar
public void withProgressBar() {
Alerter.create(this).setTitle("Geeks For Geeks")
// enables the progress
.setText("A portal for Computer Science Student").enableProgress(true)
// set color of the progress
.setProgressColorRes(R.color.purple_200)
.show();
}
public void withVisibilityCallbacks() {
Alerter.create(this).setTitle("Geeks For Geeks")
.setText("A portal for Computer Science Student").setDuration(10000).setOnShowListener(new OnShowAlertListener() {
@Override
public void onShow() {
// showing simple toast when it is shown
Toast.makeText(MainActivity.this, "On Show ", Toast.LENGTH_SHORT).show();
}
}).setOnHideListener(new OnHideAlertListener() {
@Override
public void onHide() {
// showing simple toast to user when it is hidden
Toast.makeText(MainActivity.this, "On Hide ", Toast.LENGTH_SHORT).show();
}
}).show();
}
}
Output:
Similar Reads
How to create popup message using Alerter Library in android
In this article, we learn about how to create a popup message with the help of Alerter Library. It is better to use Alerter than using Toast or Snackbar in cases if some alert messages are to be displayed to the user. We can add various onClickListners to our alerter message which makes it better an
2 min read
Implementation of WhatsNew Library in Android
In this article, we are going to show the important content of the app using the WhatsNew library. It can be simply thought of like showing some information like notification, instructions, faq, or terms and conditions like things. A sample GIF is given below to get an idea about what we are going t
3 min read
How to Implement Loading AlertDialog in Android?
AlertDialog is defined as the small window that shows a particular message to the user when the user performs or commits certain action. In this article, we are going to build a simple android application in which we learn how to implement a Loading AlertDialog that means whenever the user clicks on
6 min read
Implement Google Admob Banner Ads in Android using Kotlin
There are several ways to earn money from mobile applications such as selling products online through applications or simply displaying ads within applications. There are so many types of ads that we can display within our application. Google provides us a service known as Google Admob with the help
3 min read
How to Create AlertDialog Box Using SweetAlert Dialog Library in Android?
In this article, we will learn about how to add Custom Alert Dialog in an app using the SweetAlert Dialog Library. It is a pop-up box that appears in response to any action of the user. AlertBox is very useful when it comes to validation, it can be used to display confirmation messages. Suppose the
4 min read
How to Implement a Video Player Inside an AlertDialog in Android?
Mainly alert dialogues are used to display an important message to the user. It is possible to embed a video player inside it. This allows developers to create dynamic and interactive video-based dialogs that are useful in various use cases. In this article, we will explore the step-by-step process
3 min read
Detect Different Types of Touch Gestures in Android using Jetpack Compose
Most of the smartphones of this generation have a touchscreen which serves as a medium for input as well as output. Users can click the screen for giving inputs to the device and the same screen is used for displaying the output for the given action. A user may touch different points on the screen a
3 min read
How to Implement RecyclerView in a Fragment in Android?
In Android, a fragment is a modular section of a user interface that can be combined with other fragments to create a flexible and responsive application. Â A fragment represents a behavior or a portion of the user interface in an Activity, which can be reused in different activities or layouts. It h
12 min read
How to Implement onBackPressed() in Fragments in Android?
In Android, the Fragment is the part of the Activity that represents a portion of the User Interface(UI) on the screen. It is the modular section of the android activity that is very helpful in creating UI designs that are flexible in nature and auto-adjustable based on the device screen size. onBac
3 min read
AlertDialog in Android using Jetpack Compose
AlertDialog shows the Alert message and gives the answer in the form of yes or no. Alert Dialog displays the message to warn you and then according to your response, the next step is processed. Android Alert Dialog is built with the use of three fields: Title, Message area, and Action Button.We have
3 min read