Dialog: confirmation when dismissed - android

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

Related

Hide dialog in kotlin on certain conditions

Is it possible to achieve this in Kotlin Android studio.
I have two alert dialog
Dialog one shows on one event to notify users that the event is loading.
Dialog 2 shows when the first event finish loading.
The current results, if dialog one shows, within 2 seconds the activity finish loading and then shows dialog 2.
How can I hide dialog one, on condition of when dialog 2 shows?
This is my alert dialog code
AlertDialog.Builder(requireActivity())
.setTitle("Status upload")
.setMessage("Your image was successfully uploaded!") // Specifying a listener allows you to take an action before dismissing the dialog.
// The dialog is automatically dismissed when a dialog button is clicked.
.setPositiveButton(android.R.string.yes) { dialog, which ->
// Continue with delete operation
} // A null listener allows the button to dismiss the dialog and take no further action.
.setIcon(R.drawable.ic_check_success)
.show()
you can do this where the event ends
dialog1.dismiss()

How to show the same DialogFragment twice or more

My application has a button that shows a custom DialogFragment when the user presses a button. The DialogFragment is shown like this:
if(searchDialog == null)
{
searchDialog = new SearchElementDialog();
searchDialog.setOnItemClickListener(searchElementItemClickListener);
}
searchDialog.show (getFragmentManager(), "SearchElement");
When the user is finished dismiss is called in the usual way and the dialog is removed. Now, when the user presses the same button again I want to show the same dialog, in the same visible state as when the user left it, calling the code in the first block above does display the dialog and its visual state is restored, but:
The screen is not dimmed, and pressing outside of the dialog does not dismiss it and neither are the controls behind it responding.
The dialog does not move when the softkeyboard comes into view.
On the first showing everything is fine. Is it not possible to show the same instance of a dialog again?
What if you hide and show the dialog instead of the whole fragment.
When you want to dismiss call:
searchDialog.getDialog().dismiss();
and when you want to show it:
searchDialog.getDialog().show();
Simas was partly right. getDialog() doesn't return null when the Dialog is shown but it does when there is no Dialog displayed. I stored it as a field in my class
mDialog = getDialog()
and subsequently called
mDialog.show()
which worked every time.

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.

Custom Dialog - Login - back button?

I'm trying to setup UI where user will have to login first time they use applications. And Custom Dialog seems like a good thing to use as I want main UI to be kind of visible on background.
So, what I did - I created main Activity and use ShowDialog() with onCreateDialog from main activity.
I created
public class LoginDialog extends Dialog implements View.OnClickListener
and I can control all stuff on dialog just like activity.
Ideally I like to check if user logged in on main activity and if not - show this dialog. Otherwise just go with activity.
On dialog I'd like to log user in and if user clicks back without logging in - I want to detect it and just finish main activity. This is where I have problem.
In WinForms I would do ShowDialog() (in C#) and next line executed when dialog closed for any reason. I can't figure out how to do this in Android.
I didn't get to it yet, but I want to show progress bar when Login button clicked. This bar will be in Dialog box. Is it possible/doable?
Thanks in advance!
You can detect dialog dismissal using setOnDismissListener. In this method you could also call MyActivity.this.finish().
For showing a ProgressBar during the login process, you probably want to look at this answer. It shows the basic structure of AsyncTask and you can adapt it to use ProgressBar instead of ProgressDialog.
You would be changing the bar's visibility in onPreExecute and onPostExecute with bar.setVisibility(View.VISIBLE) and bar.setVisibility(View.INVISIBLE).
Edit
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
dialog.dismiss();
if (!isLoggedIn()) {
MyActivity.this.finish();
}
}
});
This code should be in your MyActivity wherever you create the dialog. You need to check to see if the user is logged in or not, because onDismiss will be called whether it's the user or your own code that closes the dialog.

Categories

Resources