How to Display a ListView inside AlertDialog in Android?
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.
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
<? xml version = "1.0" encoding = "utf-8" ?> < androidx.constraintlayout.widget.ConstraintLayout android:layout_width = "match_parent" android:layout_height = "match_parent" > < ListView android:id = "@+id/list_view_1" android:layout_width = "match_parent" android:layout_height = "match_parent" /> </ androidx.constraintlayout.widget.ConstraintLayout > |
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
<? xml version = "1.0" encoding = "utf-8" ?> < RelativeLayout android:layout_width = "match_parent" android:layout_height = "match_parent" tools:context = ".MainActivity" > < Button android:id = "@+id/button_1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_centerInParent = "true" android:text = "click" /> </ RelativeLayout > |
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.
Kotlin
package org.geeksforgeeks.lvinad import android.app.AlertDialog import androidx.appcompat.app.AppCompatActivity 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?) { super .onCreate(savedInstanceState) setContentView(R.layout.activity_main) // 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>(R.id.button_1) // 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>(R.id.list_view_1) // Adapter is created and applied to ListView val mAdapter = ArrayAdapter( this , android.R.layout.simple_list_item_1, mCountry) mListView.adapter = mAdapter mAdapter.notifyDataSetChanged() // Row item is set as view in the Builder and the // ListView is displayed in the Alert Dialog mAlertDialogBuilder.setView(mRowList) val dialog = mAlertDialogBuilder.create() dialog.show() } } } |
Output:
You can see that when the button is clicked, the Alert Dialog is displayed with ListView inside it.