In this android example tutorial, we will see how to show an Alert Dialog by using Kotlin in android studio.
What is Alert Dialog?
Android AlertDialog is a subclass of Dialog class. It's used to force a user to make a decision in a small dialogue box before continuing with the same activity without switching screens. A dialog shows the title, message, up to three buttons or a custom layout. Through alert dialog, we create positive (yes), negative (no) and neutral (cancel) decision buttons.
Implementation:
To begin, Create a new project in android studio and enter the name, select the project location and language to Kotlin. Then press the Finish button.
Open the activity_alert_dialog.xml file and add the LinearLayout with orientation (Vertical or horizontal).
<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:gravity="center"
tools:context=".AlertDialogActivity">
</LinearLayout>
|
In activity_alert_dialog.xml file, insert an Button inside a LinearLayout.
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:gravity="center"
android:text="Show Alert Dialog"
></Button>
|
Add id of Button by using id atribute and text using text attribute
android:id="@+id/btn"
android:text="Show Alert Dialog"
|
Button in android has the following properties:
id, padding, textSize, text, textColor, background, layout_margin etc.
Now go to AlertDialogActivity.kt and add the unique id of Button which is available in activity_alert_dialog file.
val btn = findViewById<Button>(R.id.btn)
|
Set the onClickListener event on the button instance
btn.setOnClickListener{
}
|
Create a variable instance of dialog inside button onclick method.
val dialog = AlertDialog.Builder(this)
|
Then set a title, message on dialog reference by using this
dialog.setTitle("Delete Wallpaper")
dialog.setMessage("Do you really want to delete?")
|
Now, set a positive button on a dialog instance using
dialog.setPositiveButton("Yes", DialogInterface.OnClickListener { dialog, which ->
Toast.makeText(
this,
"Hello",
Toast.LENGTH_SHORT
).show()
dialog.dismiss()
})
|
Use Negative Button in dialog
dialog.setNegativeButton("No", DialogInterface.OnClickListener{
dialog, which ->
dialog.dismiss()
})
|
For displaying the dialog on screen and close the dialog use these
dialog.show()
dialog.dismiss()
|
Now run the application in emulator or real device, you will see the output shown below.
OUTPUT:
Complete source code for Android Alert Dialog example:
activity_alert_dialog.xml file
<?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:gravity="center"
tools:context=".AlertDialogActivity">
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:gravity="center"
android:text="Show Alert Dialog"></Button>
</LinearLayout>
|
AlertDialogActivity.kt file
import android.content.DialogInterface
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
class AlertDialogActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_alert_dialog)
val btn = findViewById(R.id.btn)
btn.setOnClickListener{
val dialog = AlertDialog.Builder(this)
dialog.setTitle("Delete Wallpaper")
dialog.setMessage("Do you really want to delete?")
dialog.setPositiveButton("Yes", DialogInterface.OnClickListener { dialog, which ->
Toast.makeText(
this,
"Hello",
Toast.LENGTH_SHORT
).show()
dialog.dismiss()
})
dialog.setNegativeButton("No", DialogInterface.OnClickListener{
dialog, which ->
dialog.dismiss()
})
dialog.show()
}
}
}
|
Conclusion: We have covered the topic How to show an Alert Dialog on click of Button to Android by using Kotlin.