Clicking outside "never show again" popup dialog - android

I used the answer from Cengiz Can from here
How to make a "do not ask me again" dialog pop-up box? Android
to set up a "never show again" dialog popup. It works but when the user clicks outside of the popup dialog, it closes.
How can I block the user from clicking outside in order that my popup does not get closed.
The dialog just should get closed when the user clicks "cancel" or "Ok" and not by clicking outside the box anywhere in the layout.
Is there a method to avoid this?
Thank you

Add set builder.setCancelable(false); in your dialog builder. it will not close user click on screen apart from button.

Add this line builder.setCancelable(false);
Example
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Test");
builder.setPositiveButton("ok", null);
builder.setCancelable(false);
builder.show();

If you want to avoid closing the dialog with the back key :
setCancelable(false);
If you want to avoid closing the dialog touching outside :
setCanceledOnTouchOutside(false);

Related

Android Alert Dialog still attached to Context after click outside

Hi I have a strange Problem.
I have an Activity with a Fragment with a RecyclerView. When a user clicks on an item I show a simple alert dialog where the user can make some specifications. When the user has selected something within the dialog, I close the dialog and finish the activity to go back to the parent activity.
This works as expected but if the user selects nothing and closes the dialog with a click outside and goes back to the parent activity, leakcanary shows me that the dialog is still attached to the activity context. So my question is how to avoid that?
I have already tried to set an onCancel or an onDismissListener to dismiss the dialog myself but that is not working.
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setCancelable(true);
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.setCancelable(true);
alertDialog.setCanceledOnTouchOutside(true);
This might work.
I solved it by adding a destroy where method where I dismiss the dialog.

Dialog: confirmation when dismissed

Topic: I want to be able to cancel a dismiss call on a dialog.
I am entering information thorugh a dialog. When data inside the dialog has changed and the user dismisses the dialog without saving (by pressing back or clicking outside of the dialog), I want to be able to prevent that dismis by showing a confirmation dialog, that asks the user if he really wants do dismiss the dialog.
An analagy for what I am looking for is in VBA, where the cancel-variable of a beforeSave-listener can be set to "true", so that the file is not saved, even though save is pressed.
I could not find a solution that I can place inside the dismissListener of the dialog.
Thanks a lot in advance and best regards!
You can maintain the state of dialog open/ close in a boolean and handle it. When you open the dialog you make it true and when user clicks back or outside the dialog check that boolean and show the alert pop-up and when dialog closes (dismisses) make boolean to false.
user these methods on your dialog view to prevent cancel dialog
ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.setCancelable(false);
progressDialog.show();

How to make alertDialog not lose focus until dismiss() is called

My alertDialog closes when I minimize my app. I don't want it to until alertBuilder.dismiss() is called. Also my alertBuilder depends on some user inputs so its not possible to open it again on activity resume. So how not to lose focus of it?
EDIT: My alertBuilder has some links in it and when user presses on one of the links,it is redirected to another app and then the alertBuilder closes.I want this not to happen.Please help me.
You can setCancelable(false) on your alert dialog.
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("Your Title")
.setMessage("Your Message")
.setCancelable(false)
.setPositiveButton("OK",null)//optional
.show();
This way alert dialog does not close on activity minimize or back press.
Clicking positive or negative button finally dismisses alert dialog so these buttons might be provisions to dismiss the alert dialog.
You can supply null as onClickListener parameter, if don't want to execute any code on button click. This also dismisses alert dialog.

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);

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.

Categories

Resources