How to display Listview inside Dialog in Android | Kotlin Example

Published November 30, 2020


How to display Listview inside Dialog in Android application. In this example, we are going to create Dialog with Listview.

Let's get started

Step 1: Create a Project in Android studio

Step 2: Create an Activity and add functionality to show the dialog.

Create Alert Dialog

val builderSingle: AlertDialog.Builder = AlertDialog.Builder(this@MainActivity)

 

This AlertDialog.Builder object contains a method called setAdapter to show the List of data with Listview.

builderSingle.setAdapter(arrayAdapter, DialogInterface.OnClickListener { dialog, which ->

})

 

 

package com.rrtutors.listviewinsidedialog

import android.app.AlertDialog
import android.content.DialogInterface
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.ArrayAdapter
import android.widget.Button

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
       var btn=findViewById<Button>(R.id.btn)
        btn.setOnClickListener {

            val builderSingle: AlertDialog.Builder = AlertDialog.Builder(this@MainActivity)
            builderSingle.setIcon(R.mipmap.ic_launcher)
            builderSingle.setTitle("Select One HotSpot:-")

            val arrayAdapter = ArrayAdapter<String>(this@MainActivity, android.R.layout.select_dialog_singlechoice)
            arrayAdapter.add("HotSpot 1")
            arrayAdapter.add("HotSpot 2")
            arrayAdapter.add("HotSpot 3")
            arrayAdapter.add("HotSpot 4")
            arrayAdapter.add("HotSpot 5")
            arrayAdapter.add("HotSpot 6")
            arrayAdapter.add("HotSpot 7")
            arrayAdapter.add("HotSpot 8")
            arrayAdapter.add("HotSpot 9")
            arrayAdapter.add("HotSpot 10")

            builderSingle.setNegativeButton("cancel", DialogInterface.OnClickListener { dialog, which -> dialog.dismiss() })

            builderSingle.setAdapter(arrayAdapter, DialogInterface.OnClickListener { dialog, which ->

            })
            builderSingle.show()
        }
    }
}

 

 

Step 3: Now run the application

ListView inside Dialog in Android

 

Related: Save Image in SQLite Database Android - Kotlin


Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

912 Views

Subscribe For Daily Updates

Flutter Questions
Android Questions