I'm working on an accesibility app. When the user wants to leave the app I show a dialog where he has to confirm he wants to leave, if he doesn't confirm after 5 seconds the dialog should close automatically (since the user probably opened it accidentally). This is similar to what happens on Windows when you change the screen resolution (an alert appears and if you don't confirm it, it reverts to the previous configuration).
This is how I show the dialog:
AlertDialog.Builder dialog = new AlertDialog.Builder(this).setTitle("Leaving launcher").setMessage("Are you sure you want to leave the launcher?");
dialog.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
exitLauncher();
}
});
dialog.create().show();
How can I close the dialog 5 seconds after showing it?
then call dismiss meth it work
This is the code, refer this link:
HAPPY CODING!
Use
CountDownTimer
to achieve.Late, but I thought this might be useful for anyone using RxJava in their application.
RxJava comes with an operator called
.timer()
which will create an Observable which will fireonNext()
only once after a given duration of time and then callonComplete()
. This is very useful and avoids having to create a Handler or Runnable.More information on this operator can be found in the ReactiveX Documentation
You can cancel behavior triggered by the timer by unsubscribing from the observable on a button click. So if the user manually closes the alert, call
subscription.unsubscribe()
and it has the effect of canceling the timer.I added automatic dismiss with the time remaining shown in the positive button text to an
AlertDialog
.