Android Kotlin AlertDialog

Alert dialog is a subclass of dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout.

Lets See example .

AlertDialog.Builder


AlertDialog.Builder class has some of the important method/fun for building AlertDialog .
Dialog has three part Title,Content Area,Action

1. Icon :
 setIcon() is used to set the icon of dialog , it takes resource id of the drawable as parameter .

2. Title : 
 setTitle() is used to set the title of dialog , it takes string as parameter .

3. Message :
 setMessage is used to set the message of dialog , it takes string as parameter .

4. Adding Buttons : 
  • Positive Button : You should use this to accept and continue with the action (the "OK" action). call setPositiveButton to add positive button .
  • Negative Button : You should use this to cancel the action. call setNegative to add Negative button .
  • Neutral Button : You should use this when the user may not want to proceed with the action . call setNetural to add Netural button .
Above the three method takes two parameter .
1. String : The text to display in the positive button .
2. DialogInterface.OnClickListener: For Listening click event .

Show Dialog
Calling show() on alertDialog object will show alertDialog .

Dismiss Dialog
 On DialogInterface calling dismiss() , will dismiss the dialog.

XML Layout


Create XML Layout activity_main.xml , this will be set as content view for launcher Activity (MainActivity.kt) .

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 AlertDialog"
            android:textSize="20sp"
            android:textAllCaps="false"
            android:onClick="showAlertDialog"/>
    
</LinearLayout>

Activity


  1. Create a koltin class MainActivity.kt and extend it to AppCompatActivity class .
  2. Override onCreate function and set the content of this MainActivity with above defined xml layout (activity_main.xml).
file : MainActivity.kt
package com.tutorialsbuzz.alertdialogsample

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

class MainActivity : AppCompatActivity() {

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

    fun showAlertDialog(view: View): Unit {

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

        builder.setTitle("Title")
        builder.setMessage("Message")
        builder.setIcon(R.mipmap.ic_launcher)

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

        builder.setNeutralButton("Can't Say", { dialog, i ->
            dialog.dismiss()
        })

        val alertDialog: AlertDialog = builder.create()
        alertDialog.show()

    }
    
}



No comments:

Post a Comment