Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dismiss a custom dialog?

Tags:

android

dialog

I'm trying to make a custom dialog to show a view in this dialog. This is the Builder code:

//Getting the layout
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog_simple,
                               (ViewGroup) findViewById(R.id.rlDialogSimple));

//Change Text and on click
TextView tvDialogSimple = (TextView) layout.findViewById(R.id.tvDialogSimple);
tvDialogSimple.setText(R.string.avisoComprobar);
Button btDialogSimple = (Button) layout.findViewById(R.id.btDialogSimple);
btDialogSimple.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        //Do some stuff

        //Here i want to close the dialog
    }
});

AlertDialog.Builder builder = new AlertDialog.Builder(AcPanelEditor.this);
builder.setView(layout);
AlertDialog alert = builder.create();
alert.show();

So, i want to dismiss the dialog in the onClick of btDialogSimple. How i can do it? I don't know how to call the dismiss method from inside a onclicklistener.

My buttons have a custom layout, so i don't want to make a builder.setPositiveButton.

Any ideas?

like image 757
YaW Avatar asked May 13 '10 08:05

YaW


People also ask

How do I dismiss custom dialog?

By calling setCancelable(boolean) and setCanceledOnTouchOutside(boolean) we can set whether this dialog is cancelable with the BACK key, and whether this dialog is canceled when touched outside the window's bounds. We also implement OnCancelListener and OnDismissListener to handle the cancel and dismiss events.

How do you dismiss material dialog?

Dismissing dialogs Dialogs may be dismissed by: Tapping a "cancel" button, if one is shown. Pressing the keyboard Escape key. Tapping the scrim (Android, iOS)

How to dismiss dialog box in Android?

Option 1: AlertDialog#create(). dismiss();

How do I turn off AlertDialog?

AlertDialog generally consists of the main title, the message, and two buttons, technically termed as a positive button and a negative button. Both positive and negative buttons can be programmed to perform various actions. By default, the negative button lets close the AlertDialog without any additional lines of code.


1 Answers

You have to save the AlertDialog to your parent class property, then use something like this:

class parentClass ........ {
private AlertDialog alert=null;
........
public void onClick(View v) {
        //Do some stuff

        //Here i want to close the dialog
        if (parentClass.this.alert!=null)            
        parentClass.this.alert.dismiss();
    }
........
this.alert = builder.create();
this.alert.show();

}

like image 80
Pentium10 Avatar answered Oct 06 '22 18:10

Pentium10