21

Here is my code -

View layout = LayoutInflater.from(this).inflate(R.layout.dialog_loc_info, null);
final Button mButton_Mobile = (Button) layout.findViewById(R.id.button);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(layout);
mButton_Mobile.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
        if(builder.)
            showDialog(); // this is another dialog, nothing to do with this code
        }
    });
builder.setNeutralButton(getString(android.R.string.ok),
                         new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        dialog.dismiss();
    }
});
builder.show();
sam
  • 2,780
  • 1
  • 17
  • 30
Darpan
  • 5,623
  • 3
  • 48
  • 80
  • 6
    Mind adding a comment as to why downvoted the question? it will help me elevate quality of my questions. – Darpan Nov 25 '14 at 14:05

5 Answers5

51

You can use AlertDialog methods for that.

AlertDialog alert = new AlertDialog.Builder(context).create();

if (alert.isShowing()) {
    alert.dismiss();
}

Hope it helps.

alexgophermix
  • 4,189
  • 5
  • 32
  • 59
MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
4

An alternative approach is to use a method to generate the AlertDialog with a builder and then create the AlertDialog without showing it while setting the AlertDialog to a class variable.

Then check with .isShowing(); method

Example:

AlertDialog mAlertDialog;

public showMyAlertDialog(View layout){

    AlertDialog.Builder builder = new AlertDialog.Builder(getContext());

    builder.setView(layout);

    builder.setNeutralButton(getString(android.R.string.ok),new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
            mAlertDialog = null; //setting to null is not required persay
        }

    });

    mAlertDialog = builder.create()
    mAlertDialog.show();
}

public boolean isAlertDialogShowing(AlertDialog thisAlertDialog){
    if(thisAlertDialog != null){
        return thisAlertDialog.isShowing();
    }
}

hope that it is understood how to use this source. cheers

CrandellWS
  • 2,708
  • 5
  • 49
  • 111
0

You can check it with this:

if(alert != null && alert.isShowing()){
   alert.dismiss();
}
Naruto Uzumaki
  • 2,019
  • 18
  • 37
  • @Darpan tnx for your mention, but I used from `builder` name because equal with question owner object `AlertDialog.Builder`. – Naruto Uzumaki Nov 25 '14 at 13:55
  • 1
    here is the documentation http://developer.android.com/reference/android/app/AlertDialog.Builder.html . isShowing() function is nowhere. Try running your code may be. – Darpan Nov 25 '14 at 13:59
  • isShowing does not exist on builder – real 19 Jan 29 '15 at 19:26
0

AlertDialog extends Dialog which has isShowing().

Hint: AlertDialog.Builder creates an AlertDialog instance. :)

Pararth
  • 8,114
  • 4
  • 34
  • 51
0

For fullscreen dialog, I check if an instance of it is already being shown at android.R.id.content and then replace() it:

public void showMyDialog(
        String someArgument1,
        String someArgument2
) {
    MyDialog dialog = MyDialog.newInstance(
            someArgument1,
            someArgument2
    );

    FragmentTransaction t = getSupportFragmentManager().beginTransaction();
    t.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    Fragment content = getSupportFragmentManager().findFragmentById(android.R.id.content);
    if (content instanceof MyDialog) {
        // An instance of MyDialog is already displayed! Replace it
        t.replace(android.R.id.content, dialog).commitAllowingStateLoss();
    } else {
        t.add(android.R.id.content, dialog).commitAllowingStateLoss();
    }
}

And here is my custom fullscreen dialog with no title bar:

public class MyDialog extends AppCompatDialogFragment {

    public static MyDialog newInstance(
            String someArgument1,
            String someArgument2) {
        MyDialog dialog = new MyDialog();
        Bundle args = new Bundle();
        args.putInt("arg1", someArgument1);
        args.putInt("arg2", someArgument2);
        dialog.setArguments(args);
        return dialog;
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.my_dialog_layout, container, false);
        // do whatever you want with the strings
        // like displaying them in TextViews
        String arg1 = getArguments().getString("arg1");
        String arg2 = getArguments().getString("arg2");
        return v;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        return dialog;
    }
Alexander Farber
  • 21,519
  • 75
  • 241
  • 416