//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??
First, your Dialog needs to be in the scope of your class, so you need to declare
outside of any methods. Then, in your
onCreate()method, create the Dialog like you already have. Leave just theshow()anddismiss()calls to the OnClickListeners.Your buttons would then look like:
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);