Automatic Dialog Dismiss in Android
Last Updated :
18 May, 2025
Imagine a scenario where a dialog appears, but the user doesn't interact with it. The dialog could potentially block other actions or disrupt the user experience. To address this issue, developers can employ a technique known as automating dialog dismissal using timers. In this article, we will explore how to set up a timer to automatically dismiss dialog boxes in Android applications. A sample video is given at the end 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: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to the file. In this code, we are going to implement a Button by clicking this button an Alert Dialog will pop up and Automatically dismiss in 5 seconds.
activity_main.xml:
XML
<?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=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open Alert Box"/>
</LinearLayout>
Step 3: Working with the MainActivity.kt file
In this step, we are going to apply onClickListener to the button so that when the user clicks the button an alert dialog will open and automatically dismiss after 5 seconds. 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 and dismiss it after 5 seconds.
MainActivity.kt:
Kotlin
package org.geeksforgeeks.demo
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
class MainActivity : AppCompatActivity() {
private lateinit var alertDialog: AlertDialog
private lateinit var button: Button
private val dismissalDelayMillis = 5000L // 5 seconds
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Initialize the button
button = findViewById(R.id.button)
// Set a click listener for the button
button.setOnClickListener {
openAlertDialog()
}
}
// Function to open the AlertDialog and dismiss after 5 seconds
private fun openAlertDialog() {
val builder = AlertDialog.Builder(this)
builder.setTitle("Auto Dismiss Dialog")
builder.setMessage("This dialog will automatically dismiss in 5 seconds.")
builder.setPositiveButton("OK") { dialog, _ ->
dialog.dismiss()
}
alertDialog = builder.create()
alertDialog.show()
// Use lifecycleScope to delay dismissal
lifecycleScope.launch {
delay(dismissalDelayMillis)
if (!isFinishing && alertDialog.isShowing) {
alertDialog.dismiss()
}
}
}
}
Output:
Similar Reads
Create Instruction Dialog in Android In most android applications, you must have seen that when you open a new app it shows some instructions to users about the features of their application. Here, we are going to implement the same. Here is a sample video of what we are going to build in this application. Note that we will be using Ja
3 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
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
DialogFragment in Android Dialogs are a way to show more concise information or accessibility options without creating a new activity for users. This is quite useful when we have to show more options and stuff to the user and for that, we don't want to create a whole new screen, we want the user to stay on the same screen an
4 min read
How to Disable Back Press Button in Android? Physical keys on Android let users lock the phone, switch off the device, control volume, go back, go home and display background applications. Between all the keys, the back button is used the most in applications for navigating between activities. However, if the navigation stage reaches the start
2 min read