Android SingleChoice AlertDialog

Alert dialog is a subclass of dialog class ,In SingleChoice AlertDialog the content area of dialog contains list of items allowing users to select one option from a set .


SingleChoice AlertDialog has two kind of layout .

Text
  • List of Item Containing only Text , to have this kind of layout call  .
  • To specify the items for the list, call setItems()  by passing an array , Alernatively you can pass list adapter  , the resource id of an array (eg R.array.foo) , Cursor

Text With RadioButton
  • List of Item Containing Text and RadioButon , to have this kind of layout call setSingleChoiceItems().
  • To specify the items for the list, call setSingleChoiceItems()  by passing an array , Alernatively you can pass list adapter  , the resource id of an array (eg R.array.foo) , Cursor

Optionally you can set title ,icon ,message for the alertDialog .

1. XML Layout


file : activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="30dp"
        tools:context=".MainActivity">


    <androidx.appcompat.widget.AppCompatButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="show Single Item Dialog"
            android:textSize="20sp"
            android:onClick="singleChoiceItem"
            android:textAllCaps="false"/>


    <androidx.appcompat.widget.AppCompatButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="show Single Choice Dialog"
            android:textSize="20sp"
            android:onClick="singleChoiceDialog"
            android:textAllCaps="false"/>

</LinearLayout>

1. Activity


file : MainActivity.kt
package com.tutorialsbuzz.singlechoicealertdialog

import android.content.DialogInterface
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }


    fun singleChoiceItem(view: View) {
        val singleChoiceList = arrayOf("google", "yahoo", "bing", "yandex", "baidu", "duckduckgo")
        val builder = AlertDialog.Builder(this)
        builder.setTitle("Select Search Engine")

        builder.setItems(singleChoiceList, { dialogInterface, which ->
            Toast.makeText(this@MainActivity, singleChoiceList[which], Toast.LENGTH_SHORT).show()
        })

        // builder.create().show();
        val alertDialog = builder.create()
        alertDialog.show()
    }

    fun singleChoiceDialog(view: View) {

        val singleChoiceList = arrayOf("google", "yahoo", "bing", "yandex", "baidu", "duckduckgo")
        val builder = AlertDialog.Builder(this)

        builder.setTitle("Select Search Engine")

        builder.setSingleChoiceItems(singleChoiceList, -1,
            { dialogInterface, which ->
                Toast.makeText(this@MainActivity, singleChoiceList[which], Toast.LENGTH_SHORT).show()
            }
        )

        builder.setPositiveButton("ok", DialogInterface.OnClickListener { dialog, id ->
            dialog.dismiss()
        })
        builder.setNegativeButton("cancel", { dialog, id ->
            dialog.dismiss()
        })

        // builder.create().show();
        val alertDialog = builder.create()
        alertDialog.show()
    }

}

No comments:

Post a comment