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.
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 = ...
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With