There is a Dialog Box, how can I prevent it from being dismissed? I want not only the user enter a username but also prevent him/her from leaving the box empty?
dialog.setCancelable(false);
This function call will make your dialog dismissable by touch on the outside of the dialog. Moreover, you can control the actions of the positive and negative buttons. However if you have custom dialog with edit text in it then you should check the string that you get from edittext when user click the positive or negative button and not call dialog.dismiss(); method if string is empty. Hope it helps.
Try this code, it prevents user from dismissing the dialogue box
myDialog.setCancelable(false);
Related
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);
I am preparing a login dialog that can change to a register dialog.
It has a custom view and contains a clickable text view that I use to switch between the register display and login display.
When a user clicks it I change the title of the dialog and I display or vanish the input box for retyping the password.
However, I can't change the text or the OnClick function of the positive button.
the setPositiveButton method only exists for DialogBuilder but not for the dialog itself.
Do I have to rebuild the entire dialog or is there a way to change the button "on the fly"?
After you have created your dialog, you can access the buttons with help of this:
dialog.getButton(AlertDialog.BUTTON1) //BUTTON1 is positive button
With this you can modify the text of the button.
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.
My problem is when dialog box is showing at that time may i click outside the dialog box, at that time my dialog box is dismissed automatically. What can i do. Please send answers with code. Thanks.
Hi this line is used to dismiss dialog Box.
dialog.setCanceledOnTouchOutside(true);
This line is is used for does not dismiss dialog Box.
dialog.setCanceledOnTouchOutside(false);
I am using an AlertDialog to pop up and ask the user for some information, when the user clicks on a button.
The dialog is working fine, and I get the user info it dismisses itself.
However when the app returns focus to my primary view.. it is setting the focus to an EditText widget which causes the keyboard to appear for that View.
I don't want the Keyboard to appear unless the user explicitely puts focus on that field, so how do I change/override the default behavior, so that it doesn't autofocus on this field after the AlertDialog is dismissed.
It doesn't behave this way when the view initially inflates, the field is focused (highlighted), but the keyboard is not visible, only after the user taps on the field does the keyboard come up when the app launches. This behavior after dialog dismiss is completely different.. any ideas?
(No the EditText is not anywhere near the buttons of the alert dialog, so its not as though a second tap or something is happening accidentally or the click is being passed up the stack.)
Try doing this after the dialog is dismissed:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
You can ask the InputMethodManager to resign the keyboard when you dismiss the dialog:
EditText mValue; //The input view in your Dialog
//Close the keyboard
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mValue.getWindowToken(), 0);
Hope that Helps!
The issue was as stated in comments above, I was not dismissing the keyboard from the ALERTDIALOG, I was working on the false assumption that when the dialog was dismissed that the keyboard would be as well. This assumption was wrong.
The keyboard for the alert dialog was not dismissed, the view returned to the parent view and because the edit text in the parent view got focus the keyboard updated itself to appear properly for this view.
The attempts to dismiss keyboard in the dialog dismiss listener did not remove the keyboard because they were trying to dismiss the keyboard for fields in the parent window and at the time of their execution it was still tied to the field in the alertview.
Add the dismisskeyboard calls to the onclick events that dismissed the dialout for the edittext fields in the alertDialog resolve the problem.