Open In App

How to Prevent AlertDialog Box From Closing in Android?

Last Updated : 23 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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




<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
  
</androidx.constraintlayout.widget.ConstraintLayout>


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.

Kotlin




package org.geeksforgeeks.preventdialogfromclosing
  
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)
  
        // Declaring and initializing a 
        // Builder for the Alert Dialog
        val mBuilder = AlertDialog.Builder(this)
            .setTitle("TITLE")
            .setMessage("MESSAGE")
            .setPositiveButton("Positive", null)
            .setNegativeButton("Negative", null)
            .show()
  
        // 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()
        }
  
    }
}


Output:

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads