-1

The Current Scenario

I have a gridView in my app and every cell is a custom layout of an image and a text.

When the user touches any cell it opens a dialogue via following code

final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(layout);
        builder.show();

Now the object layout in builder.setView(layout) has the layout of a imageView , a text box and a button and is set via following code

layout = inflater.inflate(R.layout.buy_set_quantity, null);

Now when the user fills the text box and presses the button I want the alert dialogue to "go away" but I can't find a way to do this.

What have I tried

i tried using builder.dismiss() but there is no method dismiss for AlertDialog.Builder type object and the following aswell

cancel();

hide();

remove();

finish();

This question has been asked many times on stack overflow and every one says to use dismiss(); and no answer has been accepted or if there is then how?

How do you dismiss the AlertDialogue.Bulder()

What works?

Pressing the back button (from the three buttons at the bottom of screen in every android) works and the dialogue box is dismissed. But that is not how it should be. So I called the method onBackPressed(); but that not only dismisses the dialogue but also takes the user to the previous activity.

Now does anybody know any new or different method that actually works?

Uzumaki Naruto
  • 753
  • 1
  • 9
  • 27
  • Possible duplicate of [How to dismiss AlertDialog.Builder?](http://stackoverflow.com/questions/11285235/how-to-dismiss-alertdialog-builder) – Hitesh Sahu Jun 16 '16 at 16:10
  • I already saw that and have mentioned in the post that AlertDialogue.bulder doesn't have these methods and it is written in that post aswell by OP. take a look again – Uzumaki Naruto Jun 16 '16 at 16:13

2 Answers2

2

You should call create() to get the Dialog, then dismiss the Dialog. For example:

final Dialog dialog = new AlertDialog.Builder(this)
        .setView(layout)
        .create();
dialog.show();

// later, when you need to dismiss the dialog
dialog.dismiss();
chessdork
  • 1,999
  • 1
  • 19
  • 20
0

You can create an external boolean variable and override onBackPressed()

boolean isOpen = false;
void showDialogBuilder()
{
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setView(layout);
    builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
       @Override
       public void onDismiss(DialogInterface dialog) {
           isOpen = false;
       }
    });
    isOpen = true;
    builder.show();
}

@Override
public void onBackPressed()
{
    if(isOpen)
    {
        isOpen = false;
        return;
    }

    super.onBackPressed();
}
Adonys
  • 123
  • 5