Android Custom Alert Dialog With Examples

In this tutorial, we’ll be discussing and implementing Custom Alert Dialogs in our Android Application.

We’d discussed Android Alert Dialogs already here.

Alert Dialogs

Alert Dialogs by default are used to create dialogs with text messages and button actions.
Following are the setter methods that we’d already discussed in the previous tutorial.

  • setTitle
  • setMessage
  • setPositiveButton
  • setNegativeButton
  • setNeutralButton
  • setPositiveButtonIcon/setNegativeButtonIcon/setNeutralButtonIcon
  • setCancelable
  • setIcon
  • setOnDismissListener
  • setItems – shows an array of items in the form of a list inside the alert dialog
  • setMultiChoiceItems – Sets a list of items to choose from inside the alert dialog
  • setView – Used to set a custom layout view inside the alert dialog.
  • show()
  • setShowListener() – This is triggered when the alert dialog is displayed on the screen. We can do any dynamic changes in here

A ProgressDialog is an AlertDialog with the ProgressBar widget.

In the following section we’ll create different types of custom alert dialog:

  • With List of Items
  • With EditText
  • With ImageView
  • With SeekBar
  • With RatingBar

Project Structure

android-drag-drop-recyclerview-project

Code

The code for the activity_main.xml layout file is given below:

Each of the Buttons would create an AlertDialog. We’ve set the onClick method for each of them which would be defined in the MainActivity.java class.

Simple Alert Dialog

Add the following method(s) to your MainActivity.java:

This is the simplest form of AlertDialog. Let’s get onto the more interesting ones.

AlertDialog.Builder is used to build the AlertDialog.

To display the AlertDialog we can alternatively use the following code as well:

AlertDialog Showing List Of Items

In the setItems method we pass the array of strings we want to display.
which argument contains the index of the list item that is clicked.

We can customize the Button texts and style by retrieving the Button instances over the alertDialog instance and setting the text color etc.

By default, the color of the Buttons is set from the colorAccent value defined in the styles.xml

AlertDialog With Multiple Selection List

This is a better version of the simple list of items that we saw earlier.
Each of the items in the list has a CheckBoxbeside it. The isChecked boolean value returns the checkBox current state.

Once the Button is clicked, we show the list of items that were selected by converting the ArrayList populated of the selectedItems into an Array.

AlertDialog With EditText

Using the setView property we can set a custom view from the layout or do that programmatically as well.
In the below function we’ve done it programmatically.

Alert Dialog With ImageView

Here we’ll inflate a custom layout inside our setView method.
Following is the layout that’ll be shown inside the AlertDialog:

alert_dialog_with_imageview.xml

Add the following function inside your MainActivity.java class:

Alert Dialog With Seekbar

On clicking the Button we show the progress value of the SeekBar.

Alert Dialog With Rating Bar

Once you’ve added all the above methods inside the MainActivity.java class let’s build and run our application. Following is the output of the above application in action.

android custom alert dialog demo output

This brings an end to this tutorial. You can download the project from the link below:

By admin

Leave a Reply

%d bloggers like this: