Suppose you have an AlertDialog with two buttons A and B. I have found that on some devices and some versions of android, if you touch any area of the screen around the dialog, the AlertDialog disappears. On other devices you are forced to select either A or B, so there is no way I can allow the user to cancel the action without adding a third ('Cancel') option to the AlertDialog. Is there any way to determine programmatically whether the third option is required?
You can control it with dialog.setCanceledOnTouchOutside(true);
For more information :
How to dismiss the dialog with click on outside of the dialog?
Dialog dialog = new Dialog(context)
dialog.setCanceledOnTouchOutside(true);
Related
The normal way to create an alert dialog is to attach it to the current activity, like in How do I display an alert dialog on Android?
However, what I want to achieve is to create an alert dialog that is not attached to a particular activity. So that when the activity is destroyed (e.g. using finishAndRemoveTask), my dialog is still here.
Is there a way to achieve that? Also, I would like the area outside of the alert dialog to be unclickable, i.e. the user must click on the ok button of the dialog in order to navigate to other activities.
I made a custom dialog (extends Dialog) and then make the object in Activity Sample.class
Then if I click one button in the Activity and the custom dialog shows up.
Here, the problem is, since the custom dialog contains the EditText, the soft keyboard is needed and it shows up, but if I click(tab) the screen outside of the dialog, the "cancel" listener is called so that the dialog is disappeared. However, what I want to implement is when I click the screen outside of the dialog, only just keyboard disappearing. Can anyone help me?
Use setCanceledOnTouchOutside(false) on the dialog instance. This will stop dismissing the dialog. But I am not sure the soft keypad will go away with this.
use this,
dialog.setCanceledOnTouchOutside(false);
This question already has answers here:
How to avoid dismissing my progress dialog when the user touches the screen?
(6 answers)
Closed 8 years ago.
I have an Alert-dialog with 2 buttons( Yes/No )
If user click yes or no the alert dialog behave normal and get into on-click method, but if user click or touch somewhere outside of alert dialog it just disappear, and nothing will happen, Is there any way to prevent dialog from disappearing when user clicked somewhere else???
Prevent dismiss dialog box on outside touch use this
dialog.setCanceledOnTouchOutside(false);
Add this line to your AlertDialog..
alertDialog.setCanceledOnTouchOutside(false);
For more read here Dialog cancel
You must give this line.
alertDialog.setCanceledOnTouchOutside(false);
on bottom of the your AlertDialog.
Yes Use
YourAlertDialog.setCanceledOnTouchOutside(false);
use
alertDialog.setCanceledOnTouchOutside(false);
also you can use given below line for a practice that alert dialog can only go away by pressing the buttons
alertDialog.setCancelable(false).
This will make it so it can only go away by pressing the buttons.
I usually use the combination of:
alertDialog.setCancelable(false);
alertDialog.setCanceledOnTouchOutside(false);
I have an app where the user presses a button and a custom spinner is popped so that the user can choose between nine colors. I want the spinner to be dismissed, when the user touches the background (every place on the sreen except from the spinner). Is this possible?
I tried to add an onTouchListener on the image that covers the background and call
dialog.dismiss();
but it does not work.
My spinner is custom Spinner, set in an xml file and popped with:
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.colorchooser);
Thank you in advance
First of all you should be able to add
dialog.setCanceledOnTouchOutside(true);
to accomplish what you want.
But the dismiss() method should also be working, I would guess that the place your are calling dialog.dismiss() is not reachable at the current moment.
I'm developing an Android 2.2 application.
I want to show a text with an Ok button but I want to add my custom style.
What is the better choice an AlertDialog with a custom layout or a Dialog?
I'd go for AlertDialog:
It's easier to implement.
The only custom thing you have to do is an XML layout and then inflate it.
AlertDialog dialog = new AlertDialog.Builder(this)
.setView(getLayoutInflater().inflate(R.layout.custom_dialog, null))
.create();
In order to listen for UI events:
View view = getLayoutInflater().inflate(R.layout.custom_dialog, null);
Button btn = (Button)view.findViewById(R.id.the_id_of_the_button);
btn.setOnClickListener(blah blah);
AlertDialog dialog = new AlertDialog.Builder(this)
.setView(view)
.create();
You can check in android dialog docs:
The Dialog class is the base class for dialogs, but you should avoid instantiating Dialog directly. Instead, use one of the following subclasses:
AlertDialog is a lightweight version of a Dialog. This is supposed to deal with INFORMATIVE matters only, That's the reason why complex interactions with the user are limited. Dialog on the other hand is able to do even more complex things .
when would i use an Alert Dialog?
-When i just want to inform something to the user .
-When we want use for a prompt like Do you want to go back (Yes, No, Cancel. Alert dialog comes with 3 buttons which are positive, negative and neutral which are provided by default).
-When i want to prompt user for a simple value (number/date/string...)
when would i use a Dialog?
-When i want to carry on a complex process with more buttons and widgets .
-Example: