how to check alert is visible android
Solution 1
There is no isShowing() method on the AlertDialog.Builder class. There is one on the Dialog class though.
An AlertDialog.Builder is used to create an AlertDialog. Once you have an instance of an AlertDialog, you can determine whether or not it is still showing by then calling isShowing() on it.
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
AlertDialog alertDialog = alertDialogBuilder.create();
if(!alertDialog.isShowing()){
//if its visibility is not showing then show here
alertDialog.show();
}else{
//do something here... if already showing
}
Solution 2
Yes you can check it with isShowing(); method it's documented in the Android Documentation too
But in your case you need to catch the AlertDialog that build by the AlertDialog.Builder first.
so your code should be like this
AlertDialog alertDialog;
function showDialog() {
if(alertDialog == null) {
//Initial Creation will always show
//or you can just use create() if you don't want to show it at initial creation
AlertDialog.Builder alert = new AlertDialog.Builder(this);
AlertDialog alertDialog = alert.show();
else {
if(alertDialog.isShowing()) {
alertDialog.hide();
} else {
alertDialog.show();
}
}
}
Solution 3
Use this:
AlertDialog alertDialog = alert.create();
//to check if its being shown
if(!alertDialog.isShowing()){
//do something
alertDialog.show();
}
It will return true
if currently that alert dialog is being shown. So in your case, check if it returns false
, and then show it.
Hope it helps.
user3833308
Updated on June 15, 2022Comments
-
user3833308 about 2 years
how to check if my alert is already showing on screen ?
AlertDialog.Builder alert = new AlertDialog.Builder(this); alert.show();
I can maintain state by adding a flag in my code to set and reset, but if there is already a method that I can re use ?