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
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
package com.android.developer.soulutions.myapplication import android.content.DialogInterface import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.support.v7.app.AlertDialog import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) btnShowAlert.setOnClickListener { // build alert dialog val dialogBuilder = AlertDialog.Builder(this) // set message of alert dialog dialogBuilder.setMessage("Do you want to close this ?") // if the dialog is cancelable .setCancelable(false) // positive button text and action .setPositiveButton("OK", DialogInterface.OnClickListener { dialog, id -> finish() }) // negative button text and action .setNegativeButton("Cancel", DialogInterface.OnClickListener { dialog, id -> dialog.cancel() }) // create dialog box val alert = dialogBuilder.create() // set title for alert dialog box alert.setTitle("AlertDialogExample") // show alert dialog alert.show() }; } }
0 comments:
Post a Comment