Android: How to make AlertDialog disappear on clicking ok button? - android

I am asking the same question which is asked before at below links but the solution proposed in these links is not working for me so I am posting it again.
How to make an AlertDialog disappear?
Android AlertDialog always exits when I click on OK Button
How to navigate to next activity after user clicks on AlertDialog OK button?
Basically, I am creating an AlertDialog builder to notify the user for asking to enable a setting for the Usage Data Access and when the OK button is pressed then the Settings menu gets opened. When I press back button to come back on the app then the AlertDialog is still available there although I expected to be dismissed to be back on my app.
public void show_alert(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("This application requires access to the Usage Stats Service. Please " +
"ensure that this is enabled in settings, then press the back button to continue ");
builder.setCancelable(true);
builder.setPositiveButton(
"OK",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
startActivity(intent);
dialog.dismiss();
}
});
builder.show();
return;
}
Any hint what wrong could be going here?

Edit after some testing:
I tested OPs code on 6.0.1 and it behaved as expected - i.e. the dialog was dismissed after clicked 'OK'. I'll leave my initial answer below as an alternative that also works. Additional alternatives can be found here.
You can get a reference to your Alert Dialog it from your builder.show() method:
mMyDialog = builder.show();
In your onClick method:
mMyDialog.dismiss();
Full sample:
AlertDialog mMyDialog; // declare AlertDialog
public void show_alert(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("This application requires access to the Usage Stats Service. Please " +
"ensure that this is enabled in settings, then press the back button to continue ");
builder.setCancelable(true);
builder.setPositiveButton(
"OK",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
startActivity(intent);
mMyDialog.dismiss(); // dismiss AlertDialog
}
});
mMyDialog = builder.show(); // assign AlertDialog
return;
}

Related

Android Toast text on next activity

I have an intent and I want a toast to show as soon as we get to that intent. Code:
Toast.makeText(this,"Please enable internet connection",Toast.LENGTH_LONG).show;
startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
As you can see im going to the android setting, and I want it to prompt them to activate WiFi. Any ideas or better ways to do this?
I would rather recommend to show a dialog which asks the user to enable the wifi connection for him and then call WifiManager.setWifiEnabled() to switch on wifi.
This way, you don't have to leave your app. It is not possible to show an toast from an external activity.
Quick alternate approach:
Don't show a toast. Display a dialog saying "Please enable internet connection" and when the user presses the "OK" button fire the intent to send him to the settings page.
You could use a dialog, here's an example
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("WiFi Settings");
builder.setMessage("Please enable internet connection");
builder.setCancelable(false);
builder.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
// Launch settings activity
startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS)););
}
});
builder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
dialog.cancel();
}
});
builder.create().show();

Video call got disconnect due to do not keep activities check in android device

ongoing video call got disconnected when it goes to on pause because of do not keep activities checked under developer option in android device how can a user stay in video instead of that option is checked
You can check "Is the do not keep activities is checked or not" by using below line,
int value = Settings.System.getInt(getContentResolver(), Settings.System.ALWAYS_FINISH_ACTIVITIES, 0);
If it returns one, create a AlertDialog and ask the user to disable it first, if user clicks on positive button then create an intent for the Settings like below,
int value = Settings.System.getInt(getContentResolver(), Settings.System.ALWAYS_FINISH_ACTIVITIES, 0);
if (value == 1)
{
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set title
alertDialogBuilder.setTitle("");
// set dialog message
alertDialogBuilder
.setMessage("You need to disable `do not keep activities` option, press YES")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.dismiss();
startActivity(new Intent(android.provider.Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS));
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
I am entirely not sure if I got you correctly.
But does this: https://developer.android.com/training/scheduling/wakelock.html
answer your question?
It prevents your app from entering onPause, when a voice-chat is ongoing.

Displaying alert dialog only after a specific activity page

I have a login page.When the user logs in correctly,I want to show an alert dialog saying that your login details have been verified.Click continue to proceed.Now I want this alert dialog to be shown on the next activity page after login.
Here is the alert dialog code-:
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean dialogShown = settings.getBoolean("dialogShown", false);
if (!dialogShown) {
AlertDialog.Builder builder = new Builder(Myperwallet.this);
builder.setTitle("Fast Cashier!");
builder.setMessage("Logging In");
builder.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
//continue activity here....
}
});
builder.create().show();
// AlertDialog code here
}
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("dialogShown", true);
editor.commit();
I tried this but now alert dialog won't display after I have logged out.I need to know how can I set the flag to true when I have pressed the logout button.Here is my logout button functionality-:
logout.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
AlertDialog alertDialog = new AlertDialog.Builder(Myperwallet.this).create();
alertDialog.setTitle("FastCashier:");
alertDialog.setMessage("Are you sure you want to exit?");
alertDialog.setButton( Dialog.BUTTON_POSITIVE, "Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent logout = new Intent(Myperwallet.this,Login.class);
startActivity(logout);
}
});
alertDialog.setButton( Dialog.BUTTON_NEGATIVE, "NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
});
You need to add some implementation to your current code.
You can keep a flag in sharedPrefs and initially it would be true. After showing it for first time make it false.
This is what you must have implemented right now.
So to show it again after logout reset the flag to true when user has logged out. So again when u reach to the activity the dialog would be shown.
actually could not get Under stood more issue, but you want show your AlertDialog in some Optionnale Requirement such as when Login then it show and in next page is Required but due to SharedPreferences displayed only once at the start of the application. When I logged out and then logged in I did not get any alert dialog
SharedPreferences what do do is store your specific Sting with key and you need to match with it. after loing suceessufully you need to Update SharedPreferences with some new String and need to match with it.
also You can use File to check isExit() or not and write require Sting it and Read it and match with you require condition to show alert dialog. this may Helps you.
keep track of the activity flow ,i.e when u start your application it is doing what so ever is in onCreate method ,for ur problem set flag in the on create method ,and when u switch from any other activity to this one implement the method on Restart() and clear ur previous data ....
like this...
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
//ur code
}
hope it will be helpful,

android dialog wont close after opening system location settings

I have a dialog that prompts users to enable gps location if it's not enabled.
After opening the settings, and the user enableing gps, and pressing the back button from the location settings screen they come back to the app, but the dialog is still visible.
Here is the code for the button clicked.
// On pressing Settings button
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
dialog.dismiss();
}
});
My question is why does the dialog.dismiss() not close the dialog, I have also tried dialog.cancel() with same result. Is there something I should be doing after opening the settings screen?
Thanks,
I have exactly this code in my Activity:
private void showLocationDisabledInfo() {
final Context c = this;
AlertDialog.Builder builder = new AlertDialog.Builder(c);
builder.setMessage("TEST");
builder.setPositiveButton("OK", new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
c.startActivity(intent);
}
});
builder.setNeutralButton(R.string.cancel, null);
builder.create().show();
}
and it does close the dialog automatically, no matter what button I click. It's from my application, and it was tested on devices with APIs 8 and 10, and emulator running API 17. The only difference (possibly) between our codes is the value of mContext. Please provide the whole code responsible for setting up the dialog and the environment in which you've seen described behavior.

DialogResult in normal java class and return result

I am working on an android project where I am trying to show a AlertDialog in a separate normal java class and return the result that the user enters. I can display the dialog fine but the problem I am having is it always returns the value before the dialog has had one of the buttons pressed.
Below is the code that calls the function in the standard java class to show the dialog
private void showDiagreeError()
{
Common common = new Common(this);
boolean dialogResult = common.showYesNoDialog();
Toast.makeText(getApplicationContext(), "Result: " + dialogResult, Toast.LENGTH_LONG).show();
}
And below is the code that shows the actual dialogue
public boolean showYesNoDialog()
{
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Are you sure you do not want to agree to the terms, if you choose not to, you cannot use Boardies Password Manager")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialogResult = true;
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialogResult = false;
}
});
AlertDialog alert = builder.create();
alert.show();
return dialogResult;
}
dialogResult is a global variable visible throughout the class and being set to false. As soon as the dialog is shown the toast message is shown showing the result is false, but I was expecting the return statement to block until the user has pressed one of the buttons too set the variable to the correct value.
How can I get this to work.
After many hours hunting through the inner depths of google pages, I found this Dialogs / AlertDialogs: How to "block execution" while dialog is up (.NET-style).
It does exactly the job I was after and tested to make sure there are no ANR errors, which there isn't

Categories

Resources