tags:

views:

33

answers:

3

I have an alert dialog which contains a button. The button is defined in some XML and the layout is set using Dialog.setContentView().

The button has a listener class and I want to know how, if at all, I can access the dialog from the onClick(View v) method.

The reason for this is simply that I want to be able to dismiss the dialog - so if there's an easier/better way to do this then that would be useful to know!

A: 

Simple solution using the onCreateDialog() method from the Activity class:

// member variable
Dialog mDialog;

protected Dialog onCreateDialog(int id) {
    Builder builder = new AlertDialog.Builder(mContext);
    switch (id) {
        case DELETE_ALL_DIALOG:
            builder.setCancelable(false);
            builder.setPositiveButton(R.string.ok, new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //do what you want
                }
            });
            builder.setNegativeButton(R.string.cancel, new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dismissDialog(DELETE_ALL_DIALOG); // thats what you are looking for
                }
            });
            builder.setMessage(R.string.delete_all_bookmarks_question);
            mDialog = builder.create();
            mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            return mDialog;
        default:
            return super.onCreateDialog(id);
    }
}
WarrenFaith
This works but seems like unnecessary logic when really the dialog should be extracted out as a custom class if you ask me :)
BeRecursive
A: 

Create a custom dialog class and define the button in there. Something like:

public class CustomizeDialog extends Dialog implements OnClickListener {
    Button okButton;

    public CustomizeDialog(Context context) {
        super(context);
        /** 'Window.FEATURE_NO_TITLE' - Used to hide the title */
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        /** Design the dialog in main.xml file */
        setContentView(R.layout.main);
        okButton = (Button) findViewById(R.id.OkButton);
        okButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        /** When OK Button is clicked, dismiss the dialog */
        if (v == okButton)
            dismiss();
    }
BeRecursive
I should point out that my listener is currently a seperate Class and not an inner one
Paul Hunnisett
Then you'd have to pass a reference as you have done. Have you considered using the AlertDialog.Builder and maybe using setNegativeButton(CharSequence text, DialogInterface.OnClickListener listener)
BeRecursive
A: 

For now I've simply passed a reference to the dialog into the listener class and then called dismiss() at the appropriate time.

This doesn't seem like the best way - is there a better one?

Creating a custom dialog seems unnecessary to me... It doesn't need anything out of the ordinary.

Paul Hunnisett
Looks like that is the best way if you don't want to encapsulate the dialog in a separate class. Would have to be that or a global dialog in the activity class
BeRecursive