![]() |
VOOZH | about |
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.
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.
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:
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: