How to create an alert dialog with multiselection in Android(Kotlin)

How to create an alert dialog with multiselection in Android using Kotlin:

In this post, we will learn how to create an alert dialog in Android with a multiselection list. The user can select one or multiple items from the list. Each list item will have one checkbox to the right and we can set the list as single choice or multi choice.

Android provides a way to create selection list easily. We can load the list from an array of items. You can also go through our last tutorial to get an overview on it:

We will use kotlin here.

Create an array of items:

The first step is to create an array of items that we are showing in the alert dialog. If you are following our last tutorial, you can skip this part.

To create an array of items,

  • In the res -> values folder, right click and click on new. Click on values resource file. Set a name of the file and click on OK to create that file. I am creating the file with popup_array.xml name.
  • In this file, we will keep the array of items to show in the popup dialog.

Paste the below code in that file:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="popup_array">
        <item>Selection 1</item>
        <item>Selection 2</item>
        <item>Selection 3</item>
        <item>Selection 4</item>
        <item>Selection 5</item>
        <item>Selection 6</item>
    </string-array>
</resources>

You can put as many items as you want. It will increase the length of the list and the list will become scrollable once the maximum height of the phone is reached.

Create the dialog fragment class:

Create one new file MyDialog.kt and put the below code:

import android.app.Dialog
import android.content.DialogInterface
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.DialogFragment
import java.lang.IllegalStateException

class MyDialog : DialogFragment() {
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        return activity?.let {
            val checkedItems = ArrayList<Int>()
            val alertBuilder = AlertDialog.Builder(it)

            alertBuilder.setTitle("Select an option")
            alertBuilder.setMultiChoiceItems(R.array.popup_array, null, DialogInterface.OnMultiChoiceClickListener { dialog, index, checked ->
                if (checked) {
                    checkedItems.add(index)
                } else if (checkedItems.contains(index)) {
                    checkedItems.remove(index)
                }
            })
            alertBuilder.setPositiveButton("OK", DialogInterface.OnClickListener { dialog, id -> Log.d("DialogLog", "Ok pressed with checked items : " + checkedItems) })
            alertBuilder.create()
        } ?: throw IllegalStateException("Exception !! Activity is null !!")
    }
}

Here,

  • checkedItems is an array list of integers. This array list is used to keep the ids of all the selected items.
  • alertBuilder is the builder of AlertDialog. we are calling setMultiChoiceItems and passing the array to create the multiple choice items dialog.
  • OnMultiChoiceClickListener is called on any item clicked by the user.
  • We are checking if the item is checked or not. If it is checked, we are removing that item index from checkedItems. Else, we are adding the index to checkedItems.
  • This dialog has only one positive button OK. On clicking this item, we are printing the checked items index values i.e. checkedItems. You can check on the logcat window to see the indices.

You can show this alert dialog from any other Activity or Fragment as like below:

MyDialog().show(supportFragmentManager, "mydialog")

Android alert dialog multi selection

You might also like: