I have the following radio button dialog which works the way i want i also have size 12 set as the default as well but what i need to now be able to do is save the instancestate that is when something else is selected i want that size to be selected when the app is opened again. Here is my code
final CharSequence[] items = {"12m", "16m", "20m"};
AlertDialog.Builder builder = new AlertDialog.Builder(Tweaks.this);
builder.setTitle("Select a size");
builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if(items[item] == "12m"){
Toast.makeText(this, "your size is 12", Toast.LENGTH_SHORT).show();
}
if(items[item] == "16m"){
Toast.makeText(this, "your size is 16", Toast.LENGTH_SHORT).show();
}
if(items[item] == "20m"){
Toast.makeText(this, "your size is 20", Toast.LENGTH_SHORT).show();
}
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
}).show();
Thank you for any help
How to save the state of an Android CheckBox when the users exits the application?
However keep in mind this is a controlled save state. If your program should be killed due to lack of resources, you should save all appropriate info during onSaveInstanceState () and onRestoreInstanceState ()
Save the last selected instance state in perference file using SharedPreferences, then in your code always read the state from preference file when open the dialog, if no perference file existing, then use default.
Related
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.
I have a language setting in my android app. I save the current language into SharedPreferences and I have a onSharedPreferencesChanged that restarts all activites in the stack so they can be shown in the correct language.
This works like a charm but the language setting is being shown into a Dialog, so when I click on the language I want to change it to, shared preferences changes and then I get a Leaked Window error.
public void showDialog(View v) {
final CharSequence[] items = { res.getString(R.string.english),
res.getString(R.string.spanish) };
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(res.getString(R.string.change_language));
builder.setSingleChoiceItems(items, selected,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
switch (item) {
case 0: // English
if (selected == 0) {
Toast.makeText(
SettingsActivity.this,
res.getString(R.string.current_language),
Toast.LENGTH_LONG).show();
break;
}
changeLocale("en");
break;
case 1: // Spanish
if (selected == 1) {
Toast.makeText(
SettingsActivity.this,
res.getString(R.string.current_language),
Toast.LENGTH_LONG).show();
break;
}
changeLocale("es");
break;
}
alert.dismiss();
}
});
alert = builder.create();
builder.show();
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
if (key.equals("language")) {
restartActivity();
}
}
This method is in the parent activity of all of my activities:
public void restartActivity() {
Intent intent = getIntent();
finish();
startActivity(intent);
}
Im getting a leaked window in the line builder.show() because I'm restarting all the activities when the shared preferences change. How can I show the dialog and restart my activities without getting this error?
Thanks in advance
Explaining Abdallah's answer try to move dismiss() up as the first statement in your onClick callback method.
EDIT: First of all after you call alert = builder.create(); you should call alert.show() and not builder.show(). Furthermore having the modifications mentioned in comments you should not receive the error no more.
Your code is trying to show a Dialog after exiting the Activity.
To prevent this, you should dismiss() your Dialog before restarting Activities.
I have an application that uses data stored on the SD card, but my problem, I want to display a dialog box with error message before the application terminates. I tried to create a dialog box not in activity but in a simple class. the code that i use is unkown. for the first part and the second every thing is ok. when the SD card is installed and when i use a nexus google Tablet. I would like to have a message that their is no SDCard before that application crashed, or to start an other Activity used to tell the user that application need an Sdcard. the code that i use is given below. My application print the Log the crash when their is no SDcard.
public File getRootDirectory()
{
if (this.rootDirectory == null)
{
File sdCardRoot = MainApplication.getInstance().getSDCardRootDirectory();
if (sdCardRoot != null)
{
this.rootDirectory = sdCardRoot;
}
else if(Build.BRAND.equals("google"))
{
this.rootDirectory = Environment.getExternalStoragePublicDirectory(MainApplication.Appli_DIRECTORY);
} else {
Log.d(CLASS_TAG, " their is no carte Sd ");
/**
* I am trying to start a Fail Activity
*/
//Intent intent = new Intent(context, FailActivity.class)
//.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//context.startActivity(intent);
}
Log.i(CLASS_TAG, "Root directory set to :" + this.rootDirectory.getAbsolutePath());
}
return this.rootDirectory;
}
Try this
boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
if(!isSDPresent)
{
// yes SD-card is present
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Sorry,SD Card not Found")
.setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
});
builder.setCancelable(false);
// Create the AlertDialog object and return it
builder.create();
builder.show();
}
Probably you must create the alertbox in the UI Thread.
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
I am creating one application for student. I need to set the different message whenever
user open application.I don't understand how to do this or which method use for this.
I search lot of articles but i didn't found anything.
So please provide me some reference or code.
You can store your messages using any storage like sqlite,file or sharedprefrences and retrive message randomly on app opning..
Save your messages in a persistent storage. In android, you could use SharedPreference http://developer.android.com/reference/android/content/SharedPreferences.html, or a Sqlite databse http://developer.android.com/reference/android/database/sqlite/package-summary.html depending on your specific need. Store the messages in either of them and read back a different message each time.
Store some msgs in a SharedPreference at some point in your Activity:
SharedPreferences pref = getPreferences(Context.MODE_PRIVATE);
Editor ed =pref.edit();
ed.putString("0","msg0");
ed.putString("1","msg1");
ed.putString("2","msg2");
ed.putString("3","msg3");
ed.commit();
Then in onCreate(), retrieve a random sg and diplay to the user:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences pref = getPreferences(Context.MODE_PRIVATE);
Random r = new Random();
String msg = pref.getString(r.nextInt(4)+"", "none");
Toast.makeText(this, msg, Toast.LENGTH_LONG ).show();
}
You should read the about the fundamentals of android, this would guide you on how to do this. You're not going to spout garbage at the user, there will be a pattern. Once you find the pattern then you turn that logic into java.
Use AlertDialog
Code example below taken from:
http://www.mkyong.com/android/android-alert-dialog-example/
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
// set title
alertDialogBuilder.setTitle("Your Title");
// set dialog message
alertDialogBuilder.setMessage("Click yes to exit!").setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, close
// current activity
MainActivity.this.finish();
}
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();