Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android close custom dialog

Tags:

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

like image 329
user639410 Avatar asked Jun 06 '11 21:06

user639410


People also ask

How do I turn off custom dialog?

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

How to dismiss custom dialog in android?

By calling setCancelable(boolean) and setCanceledOnTouchOutside(boolean) we can set whether this dialog is cancelable with the BACK key, and whether this dialog is canceled when touched outside the window's bounds. We also implement OnCancelListener and OnDismissListener to handle the cancel and dismiss events.

How to close a dialog in android?

You can use the methods cancel() or dismiss() . The method cancel() essentially the same as calling dismiss(), but it will also call your DialogInterface.

How do I dismiss custom dialog Kotlin?

we need to show this dialog so we call show() with the builder variable. There is a function called setCanceledOnTouchOutside() for canceling the dialog by touching. It will take a boolean parameter. If you don't want to cancel or dismiss the dialog by touching outside of the dialog then use False for a parameter.


1 Answers

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.

like image 99
Alex Gitelman Avatar answered Sep 21 '22 20:09

Alex Gitelman