How to Set Buttons Inside an Alert Dialog in Android?
Last Updated :
24 Apr, 2025
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 orientation Vertical-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:gravity="center"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!-- Button -->
<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)
// find the button by it's ID
val open_btn:Button=findViewById(R.id.open)
// Apply OnClickListner to the Button
open_btn.setOnClickListener {
showDialog(this,"Alert Dialog","This is a Alert Dialog with Two Buttons in Android")
}
}
// funtion to Create an Dialog and set two Buttons inside it
fun showDialog(context: Context, title: String, mess: String) {
// Create a new instance of the AlertDialog.Builder class
val builder = AlertDialog.Builder(context)
// Set the tittle
builder.setTitle(title)
// Set the message of the dialog
builder.setMessage(mess)
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
// Close or dismiss the alert dialog
dialog.dismiss()
Toast.makeText(this,"You press Cancel button",Toast.LENGTH_SHORT).show()
}
builder.setCancelable(false)
val dialog = builder.create()
dialog.show()
}
}
Output:
Similar Reads
How to Add a Third Button to an Android Alert Dialog?
An Alert Dialog in Android is a small window that pops up on the screen to display important information, warnings, or confirmations to the user. It can be used to prompt the user for input, provide a message, or display an error. Step by Step Implementation Step 1: Create a New Project in Android S
3 min read
How to Create an Alert Dialog Box in Android?
An Android Alert Dialog is a UI element that displays a warning or notification message and asks the user to respond with options such as Yes or No. Based on the user's response, appropriate actions are executed. Android Alert Dialog is built with the use of three fields: Title, Message area, and Ac
4 min read
How to Create Buttons Inside a Widget in Android?
PrerequisitesHow to Create a Basic Widget of an Android App?How to Create a Dynamic Widget of an Android App?A Widget is a mini version of an Application, that provides a ground for the user to navigate through it or use its features from the Home Screen or Lock Screen. Widgets contain elements acco
4 min read
How to Change Alert Dialog Position on Screen in Android?
AlertDialog in Android is an alert message that appears in the form of a pop-up consisting of four elements namely a title, a message, a positive button, and a negative button. The positive and the negative buttons are clickable and can be programmed for performing an action. However, the AlertDialo
2 min read
How to change Input Method Action Button in Android?
In this article, IME(Input Method Action) Option is changed in android according to our requirement. Input Method Action Button is located in the bottom right corner of the soft keyboard. By default, the system uses this button for either a Next or Done action unless your text field allows multi-lin
2 min read
How to Add Image on Floating Action Button in Android?
A floating action button (FAB) is a user interface element in the mobile application that is typically circular and floats above the main content. It usually has a prominent color and icon, and it is used to provide quick access to the most commonly used action within the app. Step-by-Step Implement
1 min read
How to Add Share Button in Toolbar in Android?
In this article, we are going to create a simple Share Button in the Toolbar in Android. The Share Button is used to share information on mail, Bluetooth, Facebook, Twitter, WhatsApp, etc to an individual person or a group on any social media. We can share any type of message like text, images, vide
4 min read
How to Make an Alert Dialog Fill Majority of the Screen Size in Android?
An Alert Dialog is a window that appears on the screen to give notifications to the user, make some decisions for the user, or enter some information from the user. An Alert Dialog is generally not full screen, but in this article, we'll be implementing something similar. Building an Alert Dialog:Ti
3 min read
How to Implement Custom Dialog Maker in Android?
In this article, we are going to make an application of Custom Dialog Maker in android studio. In this application, we can create dialogs of our own choice of style, type, and animation. What is a dialog? A dialog is a small window that prompts the user to make a decision, provide some additional in
5 min read
How to Make a Custom Exit Dialog in Android?
In this tutorial, we are going to create a Custom Exit Dialog in Android. By default, android doesn't provide any exit dialog, but we can create it using the dialog class in java. But most of the developers and also the user don't like the default dialog box and also we can't do any modification in
5 min read