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)
Related
I want to show alert when user presses home button on device(do you want to exit the app ?)
How to do it ?
You can't handle Home button click, because of Android policy (Home button click event handling android). Of course, you can use onPause()/onStop() method of your current Activity, but your application will be moved to background too quick and user will not see your dialog, I think.
Also, note that Home not closes the app - just moves to background. User usually close app by pressing Back on main activity, try to handle it:
#Override
public void onBackPressed() {
// your dialog here
}
Is is not possible for Android apps to override the functionality of the home button. The best you can do is show the dialog when the user presses back in your topmost Activity.
You can find more information at the following SO answers:
https://stackoverflow.com/a/7240268/3214339
Android Overriding home key
Write the show alert dialog code in onPause() it will work perfectly.
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 have a network monitor (using Broadcast receiver) in my app, what it does it pops an alertdialog if the phone is not connected to internet and if the phone connects back it pops another alert informing the user he is back online. The problem is every time the monitor sees a state change it creates another window that stacks on top of the other one. For example if the phone has its DATA ON/OFF 3 times it will pop 6 messages. Is there a way of dismissing an alert dialog if another one opens or what would be the best approach to overcome this scenario?
Thank you in advance, your help is very much appreciated.
Android newbie
Add a variable containing the currently visible dialog:
Alertdialog a;
and then when you want to show one:
if (a!=null) {
a.cancel();
}
a = yourDialogBuilder.show();
Hope this helps.
Dialog has an isShowing() method that should return if the dialog is currently visible. So you can use that to see if a dialog is showing and hide it with dismissDialog().
I think you should consider re-designing your notification mechanism. Using Dialog can be very disturbing to the user experience since it requires an action to dismiss and interrupt the natural flow of the app.
Consider using Toast for simple notifications or Notification if you want it to show in the statusbar. Also, you can have some icon in your application that reflects the current connectivity status e.g. green is connected and red otherwise.
To ensure only one dialog is available, you can create a static instance of your dialog in your activity and dismiss it if it is not null before showing a new dialog
In my app, the user logs in with a custom made login dialog. The user can confirm and exit the dialog in two ways:
Press the Enter/Done button in the password box.
Press the OK button.
When the user has confirmed, the provided credentials are verified. If the credentials were incorrect, the dialog will reappear. This does only work if the user presses the Enter/Done button in the password box and not if the OK button is pressed. I use the same code for both the TextView.OnEditorActionListener and the DialogInterface.OnClickListener. I've tried debugging the code and I've discovered that in both cases, the boolean android.app.Activity.showDialog(int id, Bundle args) return true, which tells if the dialog was displayed or not.
I believe your best bet is to just create a new dialog. Should'nt be to hard.
UPDATE:
Also you could set it to
setVisibility(View.GONE);
And then when you want it to be show again
View.VISIBLE
Why don't you just hide() it? Only dismiss() it when you are really done with the Dialog
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.