. f Kotlin Android – AlertDialog – Example ~ Android Developers Blog

Saturday, 5 October 2019

Kotlin Android – AlertDialog – Example

Hello Friends,
                Here is the demo Alert Dialog in Kotlin. Android AlertDialog class
                is used to display a dialog box to user with positive and negative buttons.
It Appears on top of the activity layout. You may not physically access any other
UI components of activity. It will be run on UI thread.






To Create an AlertDialog, step by step process is :

1. Create an AlertDialog Builder using the activity’s context.
2. Set message content using the builder.
3. Set Positive Button Text and Action to be taken when the button is clicked using the builder.
4. Set Negative Button Text and Action to be taken when the button is clicked using the builder.
5. Create AlertDialog from the builder.
6. You may set the title to the AlertDialog box using setTitle() method.

1. MainActivity.kt
  1. package com.android.developer.soulutions.myapplication  
  2.   
  3. import android.content.DialogInterface  
  4. import android.support.v7.app.AppCompatActivity  
  5. import android.os.Bundle  
  6. import android.support.v7.app.AlertDialog  
  7. import kotlinx.android.synthetic.main.activity_main.*  
  8.   
  9. class MainActivity : AppCompatActivity() {  
  10.   
  11.     override fun onCreate(savedInstanceState: Bundle?) {  
  12.         super.onCreate(savedInstanceState)  
  13.         setContentView(R.layout.activity_main)  
  14.         btnShowAlert.setOnClickListener {  
  15.             // build alert dialog  
  16.             val dialogBuilder = AlertDialog.Builder(this)  
  17.   
  18.             // set message of alert dialog  
  19.             dialogBuilder.setMessage("Do you want to close this  ?")  
  20.                     // if the dialog is cancelable  
  21.                     .setCancelable(false)  
  22.                     // positive button text and action  
  23.                     .setPositiveButton("OK", DialogInterface.OnClickListener { dialog, id ->  
  24.                         finish()  
  25.                     })  
  26.                     // negative button text and action  
  27.                     .setNegativeButton("Cancel", DialogInterface.OnClickListener { dialog, id ->  
  28.                         dialog.cancel()  
  29.                     })  
  30.   
  31.             // create dialog box  
  32.             val alert = dialogBuilder.create()  
  33.             // set title for alert dialog box  
  34.             alert.setTitle("AlertDialogExample")  
  35.             // show alert dialog  
  36.             alert.show()  
  37.         };  
  38.     }  
  39. }  
  40.    

Download the code from here

Hope this will helps someone.
Enjoy Coding................... :)


Mukesh Kumar

Hi Guys I am from Delhi working as Web/Mobile Application Developer(Android Developer), also have knowledge of Roboelctric and Mockito ,android test driven development... Blogging has been my passion and I think blogging is one of the powerful medium to share knowledge and ideas....

0 comments:

Post a Comment

 

Copyright @ 2013 Android Developers Blog.