tags:

views:

31

answers:

1

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.

A: 

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 = ...).

JRL
Thanks ! It works :)
Trox