VOOZH about

URL: https://www.geeksforgeeks.org/android/automatic-dialog-dismiss-in-android/

⇱ Automatic Dialog Dismiss in Android - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Automatic Dialog Dismiss in Android

Last Updated : 23 Jul, 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:


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:

Output:


Comment

Explore