Michi
Michi

Reputation: 701

How do i implement a cancel-button in a Custom Dialog without AlertDialog?

i know, similar questions have been asked, and i've already looked through everything i could find, but i didn't find any answer to this problem.

Here's the Code:

    protected Dialog onCreateDialog(int id)
    {
        Dialog dialog = new Dialog(this);
        switch(id)
        {
            case R.layout.database_feed:
                dialog.setContentView(R.layout.database_feed);
                ((Button) dialog.findViewById(R.id.discard_button)).setOnClickListener(
                    new View.OnClickListener() 
                    {
                        //@Override
                        public void onClick(View v) 
                        {
                            //dialog.cancel();
                        }
                    }
                );
                break;
        }
        return dialog;
    }

I simply want to close the Dialog on a click on R.layout.database_feed button. But i don't have acces to the dialog within the onClick-method. I really feel confused.

I don't want to use an AlertDialog or a DialogBuilder, because there are other things in the Dialog that are difficult to implement in an AlertDialog or something. Also, i already know the solution to make a separate Activity for the Dialog - but actually i want to know how it works the way i'm trying here. Moreover i have already tried to use a DialogInterface.OnClickListener() but i can't use that in the setOnClickListener(...)-Method.

Just cancelling the dialog can't be that hard... but i don't get it.

Any hint/help is appreciated!

Thx

Upvotes: 2

Views: 1213

Answers (2)

Mimminito
Mimminito

Reputation: 2853

Either store the 'dialog' as a class variable, or make it final in your method.

Upvotes: 2

Shellum
Shellum

Reputation: 3179

Change

Dialog dialog = new Dialog(this);

to

final Dialog dialog = new Dialog(this);

Then you can access dialog in your onClick() method.

Upvotes: 4

Related Questions