AlertDialog setcancelable (false) method - android

I have a problem to understand setCancelable(false) in android for dialogBox.
What does it actual mean and why should we use it? I am new to Android.

setCancelable(false) means that back key doesn't close the dialog.
If for example you want the dialog to be closed only if the user click some dialog button then both setCanceledOnTouchOutside() and setCancelable() should be set to false

setCancelable(false) means you cannot close the alert dialog after it's getting already opened
For more informations :
https://developer.android.com/reference/android/app/AlertDialog.Builder.html

Related

How can i prevent an android dialog from dismiss when user press beside the dialog?

I have an Dialog (not an AlertDialog) which should only be dismissed when user press a button.
But in my case, the dialog dismiss also when user press beside the dialog.
How can i avoid that.
Thnx for help!
Use
dialog.setCanceledOnTouchOutside(false);
where dialog is your Dialog variable
Dialog docs
setCanceledOnTouchOutside(boolean cancel) or
setCancelable(boolean flag)
These are the methods that you can use to prevent Dialog from being dismissed
Set the cancelable state to false using this: '.setCalcelable(false)'
So your dialog code would look something like this...
Dialog mDialog = new Dialog(this);
mDialog.setCancelable(false);

How to hold the activity when the dialog is open

I am working with Android, and I open a Dialog using myDialog.show(); from the Activity, this Dialog has a Button and when I click that button the Dialog closes without problem using this.hide();.
this is the part of code where I have the question :
myDialog.show();
Toast.makeText(this, "the dialog is closed", Toast.LENGTH_SHORT).show();
the Toast is displayed during myDialog is open, and I thought that when myDialog is open, this hold my Activity and the Toast can not be displayed, but it is not the case.
So what I want is that myDialog once opened hold the Activity and when it is closed the Activity continue to the next instruction which is the Toast
just Override onDismiss in your dialog and put your toast in there. onDismiss gets called when the dialog is closing.
of in your activity implement OnDismissListener and set the listener in your dialog
I think that a similar question is asked here, and the solution that proposed is to create your myDialog.setCancelable(false);
I find this here :
If you want to literally not have the function that brings up the dialog return until the dialog is closed, you're in for some trouble. That's not the way the Android UI works.....

Alert dialog is opening two times results crash of application

In my android app i am trying to click a button which does some calculation result a alert dialog. asking user yes or not.
Clicking yes perform the action and No dismiss the alert dialog.
Problem is when i am clicking the button very fast it opens two alert box some times , pressing yes in first alert dialog does its action but pressing yes on second result in crash.
What will be the best approach to solve this problem . Actually this is simple subjective logic that's why i am not adding code here .
Simply add:
if(dialog != null && !dialog.isShowing()) {
dialog.show();
}
To your onClick() method. This checks to see if the dialog is showing or not, and only shows it if it isn't already visible.
You'll have to replace dialog with whatever your instance is called.

How to disable BACKKEY when an alertbox is called

I am developing an android app in which alert box is called on click of a button.
I want that when alert box is displayed it should not disappear without answering to it. Not even by pressing BACK KEY . Only when button on alert box is pressed it should act only then otherwise not.
Thank you please reply soon Urgent.
myAlertBox.setCancelable(false);
Hi write your code inside this method
public AlertDialog.Builder setOnCancelListener (DialogInterface.OnCancelListener onCancelListener)
or
setCancelable(boolean)

How to dismiss Dialog on backKey Pressed!

Friends i have an application with an Activitiy which brings some data from Content Providers and Display it in the textViews and edittext onto the Screen. but before that it prompts me for the username and password in a dialog. i have done all the getting content Providers Stuff in the positiveButton onClick Listener of the Alert Dialog.
It works fine but problem is that if i dont enter username and just press bakc key button it closses down the Dialog Box and the Back Screen is showed without loading the content providers.
Note: I have put my code of alert dialog in the onCreate Function of that activity.
So can u guide me how should i do it that when i press back key on dialog box it also should not display my activity.
Please Help!
your dialog name here.setCancelable(false);
this is working
I would honestly just do:
mDialog.setCancelable(false); //assuming the field mDialog is your Dialog
Then make sure you have both an Okay and Cancel (Positive and Negative) buttons on your Dialog. This way, the back press will do nothing, and you can use the Cancel button to finish your activity as well if desired.
The Dialog interface provides a setCancelable() method which enables precisely this. You call that with a false value and the user won't be able to press the back button to go back to your activity.
You can use method of each activity is : onBackPressed()
http://developer.android.com/reference/android/app/Activity.html
-This is the method which you can override sub method to execute when user enter back key.

Categories

Resources