How to Set Buttons Inside an Alert Dialog in Android?
Last Updated :
07 Jun, 2023
In this article, we are going to see how can we set two buttons inside an Alert Dialog. For example, we can see when an alert dialog is pop up then their two types of buttons available one is the Ok or Positive Button another one is Cancel or Negative Button. Alert dialogs are most frequently used in Android applications to display information to the user. An Alert dialog consists of a Message, Title, and some Buttons to respond to the displayed information.
In this article, we will learn how to create an alert dialog in Android with two buttons: a positive button and a negative button. Basically, An Positive Button Confirms a user’s response and continues but A Negative Button will Dismiss the Alert Dialog Window. For Implementing these Buttons inside an Alert Dialog we have to follow several steps :
- Create an instance of the AlertDialog.Builder class.
- Set the title, message, and other properties of the alert dialog using AlertDialog.Builder class.
- Call the setPositiveButton(), setNegativeButton() methods of the AlertDialog.Builder class to set the buttons of the alert dialog.
- Implement the onClick() method for each button to define what should happen when the user clicks the button.
- Call the create() method of the AlertDialog.Builder class to create the alert dialog.
- Call the show() method of the AlertDialog class to display the alert dialog to the user.
A sample video is given below to get an idea about what we are going to do in this article.
Step By Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.
Step 2: Change the StatusBar Color
Navigate to app > res > values > themes > themes.xml and add the below code under the style section in the themes.xml file.
<item name="android:statusBarColor" tools:targetApi="l">#308d46</item>
Step 3: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to the activity_main.xml file. In this code, we are going to implement a Button by clicking this button an Alert Dialog with two buttons will pop up.
activity_main.xml:
XML
<? xml version = "1.0" encoding = "utf-8" ?>
< LinearLayout
android:layout_width = "match_parent"
android:orientation = "vertical"
android:gravity = "center"
android:layout_height = "match_parent"
tools:context = ".MainActivity" >
< Button
android:id = "@+id/open"
android:layout_width = "wrap_content"
android:text = "Click to Open Alert Dialog "
android:layout_height = "wrap_content" />
</ LinearLayout >
|
Step 4: Working with the MainActivity.kt file
In this step, we are going to apply onclickListner to the button so that when the user clicks the button an alert dialog will open with two buttons. Below is the code for MainActivity.kt. Comments are added for a better understanding of the Code. The below function is Responsible for populating the alert dialog with Two Button (i.e. Positive Button- OK and NegativeButton – Cancel).
fun showDialog(context: Context, title: String, mess: String) {
// Create a new instance of the AlertDialog.Builder class
val builder = AlertDialog.Builder(context)
builder.setTitle(title)//Set the tittle
builder.setMessage(mess)//Set the message of the dialog
builder.setPositiveButton("OK") { _, _ ->
// Implement the Code when OK Button is Clicked
Toast.makeText(this,"You press OK button",Toast.LENGTH_SHORT).show()
}
builder.setNegativeButton("Cancel") { dialog, _ ->
// Implement the Code when Cancel Button is CLiked
dialog.dismiss() //Close or dismiss the alert dialog
Toast.makeText(this,"You press Cancel button",Toast.LENGTH_SHORT).show()
}
builder.setCancelable(false)
val dialog = builder.create()
dialog.show()
}
Below is the Implementation of MainActivity.kt.
MainActivity.kt:
Kotlin
package com.example.geeksforgeeks
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super .onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val open_btn:Button=findViewById(R.id.open)
open_btn.setOnClickListener {
showDialog( this , "Alert Dialog" , "This is a Alert Dialog with Two Buttons in Android" )
}
}
fun showDialog(context: Context, title: String, mess: String) {
val builder = AlertDialog.Builder(context)
builder.setTitle(title)
builder.setMessage(mess)
builder.setPositiveButton( "OK" ) { _, _ ->
Toast.makeText( this , "You press OK button" ,Toast.LENGTH_SHORT).show()
}
builder.setNegativeButton( "Cancel" ) { dialog, _ ->
dialog.dismiss()
Toast.makeText( this , "You press Cancel button" ,Toast.LENGTH_SHORT).show()
}
builder.setCancelable( false )
val dialog = builder.create()
dialog.show()
}
}
|
Output:
Share your thoughts in the comments
Please Login to comment...