Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dismiss Custom Dialog Problem

Tags:

android

dialog

I have a dialog with a custom layout, and I try to close it when I press a button:

private void showAboutDialog() { 

   dialog = new Dialog(MainMenu.this);
   dialog.setContentView(R.layout.about_dialog);
   dialog.setCancelable(true);
   dialog.setTitle(R.string.about_title);
   dialog.show();

   LayoutInflater inflater = (LayoutInflater)      
   getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
   View layout = inflater.inflate(R.layout.about_dialog,
           (ViewGroup) findViewById(R.id.layout_root));

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

But it doesn't work.

like image 509
Trox Avatar asked Aug 16 '10 14:08

Trox


1 Answers

Your listener is not being called.

Replace:

Button closeButton = (Button) layout.findViewById(R.id.about_close_button);

with:

Button closeButton = (Button) dialog.findViewById(R.id.about_close_button);

and remove the two lines above (LayoutInflater inflater = ... and View layout = ...).

like image 105
JRL Avatar answered Sep 23 '22 05:09

JRL