AlertDialog Explanation with an Example in Android

AlertDialog can be used to display the dialog message with positive (OK) and negative (Cancel) buttons. AlertDialog helps the user to interrupt and ask the user about their option to proceed or terminate. Android AlertDialog is the subclass of the Dialog class.

Android AlertDialog is composed of three regions: Title, Content Area, and Action Buttons.

In order to make an alert dialog, we need to make an object of AlertDialogBuilder which an inner class of AlertDialog. Its syntax is given below:

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

Now we have to set the positive (yes) or negative (no) button using the object of the AlertDialogBuilder class. Its syntax is:

alertDialogBuilder.setPositiveButton(CharSequence text,

DialogInterface.OnClickListener listener)

alertDialogBuilder.setNegativeButton(CharSequence text,

DialogInterface.OnClickListener listener)

Use

AlertDialog boxes are a common UI metaphor in both mobile applications and web applications. They’re used to help users answer questions, make selections, confirm actions, and read warning or error messages.

Sometimes in our application, we wanted to ask the user about deciding between yes or no in the response to any particular action taken by the user, by remaining the same activity, and without changing the screen, we can use Alert Dialog.

Other functions

Android AlertDialog Example

Let’s see a simple example of an android alert dialog.

activity_main.xml

You can have multiple components, here we are having only a button.

activity_main.xml

<?xml version=”1.0" encoding=”utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android=”http://schemas.android.com/apk/res/android"
xmlns:app=”http://schemas.android.com/apk/res-auto"
xmlns:tools=”http://schemas.android.com/tools"
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”.MainActivity”
>

<Button
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:id=”@+id/button”
android:text=”@string/close_app”
android:padding=”10dp”
app:layout_constraintBottom_toBottomOf=”parent”
app:layout_constraintLeft_toLeftOf=”parent”
app:layout_constraintRight_toRightOf=”parent”
app:layout_constraintTop_toTopOf=”parent”
/>

</androidx.constraintlayout.widget.ConstraintLayout>

strings.xml

You can store your dialog message, button text, and title in the strings.xml file.

string.xml

<resources>

<string name="app_name">AlertDialog</string>
<string name="close_app">Close app</string>
<string name="dialog_message">AlerDialog</string>
<string name="dialog_title">GBAndroidBlogs</string>

</resources>

Activity class

Below the code to create and show the AlertDialog.

ActivityMain.java

package com.codewithgolap.alertdialog;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast;
public class MainActivity extends AppCompatActivity {

Button
closeButton; AlertDialog.Builder builder;
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
closeButton = (Button) findViewById(R.id.button); builder = new AlertDialog.Builder(this); closeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

//Uncomment the below code to Set the message and title from the strings.xml file
builder.setMessage(R.string.dialog_message) .setTitle(R.string.dialog_title);

//Setting message manually and performing action on button click
builder.setMessage(“Do you want to close this application ?”)
.setCancelable(
false)
.setPositiveButton(
“Yes”, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
Toast.
makeText(getApplicationContext(),
“you choose yes action for alertbox”,
Toast.
LENGTH_SHORT).show();
}
})
.setNegativeButton(
“No”, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Action for ‘NO’ Button
dialog.cancel();
Toast.
makeText(getApplicationContext(),
“you choose no action for alertbox”,
Toast.
LENGTH_SHORT).show();
}
});
//Creating dialog box
AlertDialog alert = builder.create();
//Setting the title manually
alert.setTitle(“AlertDialog Example”);
alert.show();
}
});
}
}

Output:

Hi everyone, myself Golap an Android app developer with UI/UX designer.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store