Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

How to Display a ListView inside AlertDialog in Android?

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

In Android, A ListView is a ViewGroup that is used to display a list of strings. This view and scrollable and each item in a ListView is clickable. An Alert Dialog is a type of alert message that can be invoked to appear on the top of the screen. An Alert Dialog generally consists of a Title, a Message, a Positive, and a Negative Button. Users can choose between these two buttons to respond to the message. These two elements are quite different from each other in terms of implementation as well as the purposes served. However, it is possible to bring them together and display one inside the other.

Display a ListView inside AlertDialog in Android

So in this article, we will show you how you could create and display a ListView inside an Alert Dialog in Android. Follow the below steps once the IDE is ready.

Step by Step Implementation

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. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Create a layout for ListView (row.xml)

Navigate to the app > res > layout and create a new layout file. Add a ListView as shown below. This layout would be displayed inside the AlertDialog.


<?xml version="1.0" encoding="utf-8"?>

Step 3: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Add a Button as shown below. This Button on-click would generate an AlertDialog with a ListView inside it.


<?xml version="1.0" encoding="utf-8"?>

Step 4: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.


package org.geeksforgeeks.lvinad
import android.os.Bundle
import android.view.View
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.ListView
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        // Creating an array of strings
        val mCountry = arrayOf("India", "Brazil", "Argentina", "Portugal"
                               "France", "England", "Italy")
        // Declaring and initializing 
        // the Button from the layout file
        val mButton = findViewById<Button>(
        // When Button is clicked,
        mButton.setOnClickListener {
            // Alert DialogBuilder is initialized
            val mAlertDialogBuilder = AlertDialog.Builder(this)
            // Row layout is inflated and added to ListView
            val mRowList = layoutInflater.inflate(R.layout.row, null)
            val mListView = mRowList.findViewById<ListView>(
            // Adapter is created and applied to ListView
            val mAdapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, mCountry)
            mListView.adapter = mAdapter
            // Row item is set as view in the Builder and the 
            // ListView is displayed in the Alert Dialog
            val dialog = mAlertDialogBuilder.create()


You can see that when the button is clicked, the Alert Dialog is displayed with ListView inside it.

My Personal Notes arrow_drop_up
Last Updated : 14 Feb, 2022
Like Article
Save Article
Similar Reads