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

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

  • setCancelable(boolean cancelable): This method sets the property that the dialog can be canceled or not
  • setMessage(CharSequence message): This method sets the message to be displayed in the alert dialog
  • setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, DialogInterface.OnMultiChoiceClickListener listener): This method sets a list of items to be displayed in the dialog as the content. The selected option will be notified by the listener
  • setOnCancelListener(DialogInterface.OnCancelListener onCancelListener): This method Sets the callback that will be called if the dialog is canceled.
  • setTitle(CharSequence title): This method set the title to appear in the dialog

Android AlertDialog Example

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

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

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.