YaW
YaW

Reputation: 12142

Dismiss a custom 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?

Upvotes: 9

Views: 12746

Answers (4)

Sazzad Hissain Khan
Sazzad Hissain Khan

Reputation: 40136

Use AlertDialog instance e.g. mAlertDialog as a global variable. And call mAlertDialog.dismiss(); inside onClick()

Upvotes: 0

Salih Kiraz
Salih Kiraz

Reputation: 33

ı am using

public  Dialog dialog;

buton_sil.setOnClickListener(new View.OnClickListener() {

            @ Override
            public void onClick(View v) {

                   DialogClose();
                   }
}):


public void DialogClose() {
        dialog.dismiss();
    }

Upvotes: 0

source.rar
source.rar

Reputation: 8080

I think a better way may be to call

dismissDialog(DIALOG_ID);

Wouldn't making the AlertDialog a class property defeat the purpose of returning a Dialog from the onCreateDialog()?

Upvotes: 7

Pentium10
Pentium10

Reputation: 207830

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();

}

Upvotes: 17

Related Questions