Android Custom Dialog With ListView | Dialog onitemclicklistener

Android Custom Dialog With ListView Tutorial welcomes you.

In this example, we will create a custom dialog with listview.

We will also implement onitemclicklistener for the dialog listview.

Developers use dialog with listview when they want to have a quick selection from the user. For example, to select the city or country of the user.

Alert dialog with listview can be used as an alternative to spinner, where user can select any item from several options.

Outcome of listview dialog

Step 1. Two XML files

First of all, make a new project in android studio.

Here, select “Empty activity” as the default template so that we can have clean work space without any pre-written code lines.

We need two XML files separately. One is for custom dialog and another is for listview layout.

Create a new XML file under res->layout directory and give it a name like dialog_listview.xml 

Add the following source code in dialog_listview.xml file.

Above file create the view layout for our custom dialog.

It contains one text view, one list view and one button.

We will inflate this file in the JAVA class to create custom dialog view.

Now time create another XML file named list_item.xml under same directory.

Following is the code block for list_item.xml file.

ListView in custom dialog will have some row items. Every row items will create it’s look and feel with help of above file.

Above file contains one text view and one view. So every row of listview will also contain them with their background colors.

Step 2. Main File codes

When you have created a new project, system must have created two files automatically.

One is activity_main.xml and another is

Write down the following code snippet in activity_main.xml

It contains two UI widgets. One is Button and another is text view.

When the user click the button, we will open the custom dialog.

Text view will hold the name of the selected car.

Now in file, add the below code structure

Looking above code confidently

Let us understand the logic behind the code of main activity.

First of all, read the below one

First line is making the object of text view class. Second line will help us to use button in our project.

Last line is making one string array. This string array contain the names of the cars.

We will fill the listview with these car names.

In onCreate() method, compiler will first use findViewById() method to find textview and button.

On click method for button is like below

As per the above code, compiler will call showDialog() method when the user touches the button.

Code for showDialog() method is as the below

First of all, compiler will create the Dialog object.

Then it will set it as non cancelable.  It means that if user touches outside the dialog, system will pop down the dialog.

.setContentView() method will give the look and feel to our custom dialog. File dialog_listview.xml will help us in this task.

Then compiler will find the Cancel button using it’s id and will implement is’s click event.

Then compiler will find the listview.

After this, it will create the array adapter to populate the listview.

Here, it is using string array variable to show them (car names) in listview.

On click event of list view is also written. When the user clicks the listview, compiler will set the value of selected car name into text view.

Download Code for android custom dialog with listview

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *