VOOZH about

URL: https://www.geeksforgeeks.org/android/alert-dialog-with-singleitemselection-in-android/

⇱ Alert Dialog with SingleItemSelection in Android - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Alert Dialog with SingleItemSelection in Android

Last Updated : 23 Jul, 2025

Alert Dialogs are the UI elements that pop up when the user performs some crucial actions with the application. These window-like elements may contain multiple or single items to select from the list or have the error message and some action buttons. In this article, it's been discussed how to implement the Alert Dialogs with the single item selection. Look at the following image to differentiate the alert dialogs with action buttons and single item selection. We will implement this project using both Java and Kotlin Programming Languages for Android.


Implementation Alert Dialog with SingleItemSelection

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.

The code for that has been given in both Java and Kotlin Programming Language for Android.


Step 2: Working with the XML Files

Next, go to the activity_main.xml file, which represents the project's UI. Below is the code for the activity_main.xml file. Comments are added inside the code to help you understand the code in more detail.

activity_main.xml:

Design UI:

👁 designandblueprint-alert-dialog-box

Step 3: Working with the MainActivity File

Go to the MainActivity File and refer to the following code. Below is the code for the MainActivity File. Comments are added inside the code to understand the code in more detail. There is a need to understand the parts of the AlertDialog with single item selection.

Have a look at the following image:

👁 output
Working with the MainActivity File

The function used for implementing the single item selection is setSingleChoiceItems which is discussed below:

Syntax:

alertDialog.setSingleChoiceItems(listItems, checkedItem[0]) { dialog, which ->

}

Parameters:

  • listItems: are the items to be displayed on the alert dialog.
  • checkedItems: the boolean array maintains the selected values as true and unselected values as false.
  • DialogInterface.OnMultiChoiceClickListener(): This is a callback when a change in the selection of items takes place.

Invoke the following code. Comments are added inside the code for better understanding.

Output:

Comment

Explore