Reject user dialog task

I have a dialog with a custom layout and I try to close it when I click the 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 that will not work.

+5
source share
1 answer

Your listener is not called.

Replace:

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

with:

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

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

+7
source

All Articles