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.....
Related
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.
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
I want to create a dialog. I create a class and class extend Dialog. I create an object from this class when click on a button from my activity and call show method. If i click on button 2 times or more, second dialog create over first dialog and third dialog create over second dialog and more. when i click on back button , my dialogs remove one by one. How can i solve this problem? Please say a recommend. Thanks
Dialog has a function named isShowing() which returns a boolean to indicate that your Dialog Show status , when your button pressed you can check that if previous Dialog isShowing(), don't instantiate it again .
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.
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.