Wednesday, April 4, 2018

android kotlin - AlertDialog setItems example

MainActivity.kt

package com.cfsuman.kotlinexamples

import android.content.Context
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.app.AlertDialog
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
import android.graphics.Color


class MainActivity : AppCompatActivity() {

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

        // Button click listener
        button.setOnClickListener{
            // Show the single choice list items on an alert dialog
            showDialog()
        }
    }


    // Method to show an alert dialog with single choice list items
    private fun showDialog(){
        // Initialize an array of colors
        val array = arrayOf("RED","GREEN","YELLOW","BLACK","MAGENTA","PINK")

        // Initialize a new instance of alert dialog builder object
        val builder = AlertDialog.Builder(this)

        // Set a title for alert dialog
        builder.setTitle("Choose a color.")

        /*
            **** reference source developer.android.com ***

            AlertDialog.Builder setItems (CharSequence[] items, DialogInterface.OnClickListener listener)
                Set a list of items to be displayed in the dialog as the content, you will be
                notified of the selected item via the supplied listener.

            Parameters
            items : CharSequence
            listener : DialogInterface.OnClickListener

            Returns
                AlertDialog.Builder This Builder object to allow for chaining of calls to set methods
        */

        // Set items form alert dialog
        builder.setItems(array,{_, which ->
            // Get the dialog selected item
            val selected = array[which]

            // Try to parse user selected color string
            try {
                // Change the layout background color using user selection
                root_layout.setBackgroundColor(Color.parseColor(selected))
                toast("$selected color selected.")
            }catch (e:IllegalArgumentException){
                // Catch the color string parse exception
                toast("$selected Color not supported.")
            }
        })

        // Create a new AlertDialog using builder object
        val dialog = builder.create()

        // Finally, display the alert dialog
        dialog.show()
    }
}


// Extension function to show toast message
fun Context.toast(message: String) {
    Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}
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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/root_layout"
    android:background="#dbeadb"
    android:padding="16dp"
    android:orientation="vertical"
    >
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Options"
        android:textAllCaps="false"
        android:layout_gravity="center_vertical|center_horizontal"
        android:gravity="center"
        />
</LinearLayout>