Skip to content
Related Articles
Open in App
Not now

Related Articles

How to Prevent AlertDialog Box From Closing in Android?

Improve Article
Save Article
  • Last Updated : 23 Jan, 2022
Improve Article
Save Article

In Android, AlertDialog is a simple alert message that appears in the form of a pop-up that consists of a title, a message, and two buttons namely positive and negative buttons. The user basically has to click on one of the two buttons to reply to the message in the AlertDialog. The negative button is generally titled “cancel” or “no” or “continue” and the positive button is some affirmative text. This is because the negative button by default has the functionality of only exiting the AlertDialog and not performing any action whereas a positive button is to be programmed for performing any action and even exiting the AlertDialog.

Prevent AlertDialog Box from Closing in Android

So in this article, we will show you how you could prevent exiting or closing the AlertDialog in Android. Follow the below steps once the IDE is ready.

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. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Keep the layout file untouched or default as we won’t require or add any new element.


<?xml version="1.0" encoding="utf-8"?>
        android:text="Hello World!"
        app:layout_constraintTop_toTopOf="parent" />

Step 3: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.


package org.geeksforgeeks.preventdialogfromclosing
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        // Declaring and initializing a 
        // Builder for the Alert Dialog
        val mBuilder = AlertDialog.Builder(this)
            .setPositiveButton("Positive", null)
            .setNegativeButton("Negative", null)
        // Changing Positive Button properties such
        // that something happens on click
        val mPositiveButton = mBuilder.getButton(AlertDialog.BUTTON_POSITIVE)
        mPositiveButton.setOnClickListener {
            // Do something
            // As we do not want the Alert Dialog to close, 
            // we will only display a Toast and do nothing else.
            Toast.makeText(this, "Can't Exit", Toast.LENGTH_SHORT).show()


You can see that when the positive button is clicked, the AlertDialog isn’t closing.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!