Ricardo
Ricardo

Reputation: 121

Can't Dismiss Custom Dialog

//About Button in the principal menu       
final Button button3 = (Button) findViewById(R.id.button3);

button3.setOnClickListener(
    new OnClickListener() {

    public void onClick(View v) {                
        //set up dialog
        Dialog dialog = new Dialog(MainMenu.this);
        dialog.setContentView(R.layout.maindialog);
        dialog.setTitle("About");
        dialog.setCancelable(true);

        //now that the dialog is set up, it's time to show it    
        dialog.show();

        Button closeButton = (Button) dialog.findViewById(R.id.Button01);
//      closeButton.setOnClickListener(new Button.OnClickListener() { 
//          public void onClick(View view) { 
//              dialog.dismiss();
//          }
//      });

        if(v==closeButton)
            dialog.dismiss();
    }
});

I have this code but the dismiss is not working.

I have an "about" button and when i click on in it shows the dialog window. Then the dialog windows has a "OK" button and this OK button should dismiss the dialog but the dismiss is not working. Could you help me to know why??

Upvotes: 1

Views: 6046

Answers (3)

Sanoop Surendran
Sanoop Surendran

Reputation: 3486

Initialize the Dialog outside the OnClickListener.

Dialog dialog = new Dialog(MainMenu.this);
dialog.setContentView(R.layout.maindialog);

button3.setOnClickListener(new OnClickListener() {           
       public void onClick(View v) {              
         dialog.show();    
       }
});

Button closeButton = (Button) dialog.findViewById(R.id.Button01);
closeButton.setOnClickListener(new OnClickListener() {
       public void onClick(View v) {
          dialog.dismiss();
       }
});

Upvotes: 1

rmdroid
rmdroid

Reputation: 899

You can also write a common onClick for both the buttons like this. This will avoid the rewriting of onClick event for every button.It will become easy for maintenance.

public void onClick(View v) {

   switch(v.getId())
   {
       case R.id.button3:
           dialog.show();

       case R.id.Button01:
           dialog.dismiss();
   }


}

Upvotes: 1

Glendon Trullinger
Glendon Trullinger

Reputation: 4062

First, your Dialog needs to be in the scope of your class, so you need to declare

public Dialog dialog;

outside of any methods. Then, in your onCreate() method, create the Dialog like you already have. Leave just the show() and dismiss() calls to the OnClickListeners.

Your buttons would then look like:

Button button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        dialog.show();
    }
});

Button closeButton = (Button) dialog.findViewById(R.id.Button01);
closeButton.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        dialog.dismiss();
    }
});

Also, it's good to come up with a naming convention that works for you rather than randomly capitalizing or not capitalizing resource names (e.g., Button03 vs. button1);

Upvotes: 7

Related Questions