21

I am trying to get the custom dialog to close on button press

        //set up dialog
        Dialog dialog = new Dialog(BrowseActivity.this);
        dialog.setContentView(R.layout.about);
        dialog.setTitle("This is my custom dialog box");
        dialog.setCancelable(true);
        //there are a lot of settings, for dialog, check them all out!

        //set up text
        TextView text = (TextView) dialog.findViewById(R.id.TextView01);
        text.setText(R.string.app_help_message);

        //set up image view
        ImageView img = (ImageView) dialog.findViewById(R.id.ImageView01);
        img.setImageResource(R.drawable.icon);

      //set up button
        Button button = (Button) dialog.findViewById(R.id.Button01);
        button.setOnClickListener(new View.OnClickListener() {
        @Override
            public void onClick(View v) {
            Dialog.dismiss();

            }
        });

        //now that the dialog is set up, it's time to show it    
        dialog.show();

       return true;

dialog.dismiss is not working for me. I am simply trying to use this custom dialog as a help screen and want a button press to close it.

I'm very new to android dev but have been trying this for many many hours

Thanks for any advise

UmAnusorn
  • 10,420
  • 10
  • 72
  • 100
user639410
  • 387
  • 1
  • 3
  • 9
  • You need to call dismiss on dialog instance (which you have created using Dialog dialog = new Dialog(BrowseActivity.this), not on a Dialog class. – A.B. Nov 04 '16 at 06:50

2 Answers2

34
final Dialog dialog = new Dialog(BrowseActivity.this);

You need lowercase dialog.

public void onClick(View v) {
   dialog.dismiss();
}

Also AlertDialog.Builder may be a better choice for you.

Alex Gitelman
  • 24,429
  • 7
  • 52
  • 49
  • well that solved the problem, I have used AlertDialog.Builder for the about screen but couldn't figure out how to set a layout for it. Like I say, it's all new to me and I'm surprised I have got this far. I will read more – user639410 Jun 06 '11 at 21:40
3

You may call dismiss(); on the dialog. This work for me.

UmAnusorn
  • 10,420
  • 10
  • 72
  • 100