Preference Activity get boolean in other activity - android

I have a PreferenceActivity and I want to get a pref value into an another activity.
<CheckBoxPreference
android:key="use_english"
android:title=""
android:summary=""
android:defaultValue="false" />
And I want to get the boolean into my MainActivity.
How I am supposed to do ?

You simply call the following lines:
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
boolean mValue = sp.getBoolean("use_english", false);
Note: The first paramter is obviously the key specified in your Preferences-XML file. The second paramter is the default value. You can read more about this in the Docs.

Related

Android - how to save CheckBox enabled state

I have some PreferenceActivity and there are some CheckBoxes.
<PreferenceScreen
android:key="prefGraphValues"
android:title="#string/pref_graph_values"
android:persistent="false" >
<CheckBoxPreference
android:defaultValue="true"
android:key="Temp.Dry"
android:summary="#string/pref_graph_value_temp_dry_summary"
android:title="#string/pref_graph_value_temp_dry" >
</CheckBoxPreference>
</PreferenceScreen>
Somwhere in code I have something like this:
checkBox.setEnabled(false);
But when I close this activity and later launch it, checkBox is enabled. Can I save this state?
Thanks
Change your persistent property to true.
android:persistent="true"
Actual, if you assign it to false it means that preference won't store value(s) to the SharedPreferences and this is reason why your state is not saved in "next launch".
You have to apply the change to sharedpreference and next time the checkbox will retrieve the value by the key from sharedpreference.
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
sp.edit().putInt(your_key, your_value).apply();

CheckBoxPreference default vale not working with SharedPreferences before you first open Preference page

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
boolean cbTest = sharedPreferences.getBoolean("checkbox_test", false);
this are my preference
<CheckBoxPreference
android:defaultValue="true"
android:key="checkbox_test"
android:summary="#string/checkbox_tes"
android:title="#string/title_heckbox_tes" />
and cbTest variable is by default always false, when I open page with preferences and then check cbTest again then is true. Why is by default always false? Why preference page needs to be initialized that start working?
How can I check default value before open preference page?
I already answer similar question but I cannot quickly located. You have to initialize your default share preferences. In your main activity put the code below in onCreate()
PreferenceManager.setDefaultValues(this, R.xml.yourfilename, false);

CheckBoxPreference Default Value

I have a CheckBoxPreference defined as follows:
<CheckBoxPreference
android:defaultValue="true"
android:key="prefVisible"
android:summary="#string/pref_visible_summary"
android:title="#string/pref_visible" >
</CheckBoxPreference>
My application uses this preference to control the visibility of a view. When I first start my application (on a new wiped emulator) the view is not shown. However, when the I go to the preferences screen (activity) the checkbox is shown as checked.
Does this mean that the defaultValue attribute is not actually setting the preference but rather it's just setting the value of the checkbox if there is no underlying data (as would be the case on a brand new install). And does this also mean that the preference is set only after the user enters/exits the preferences screen (activity) for the first time, otherwise it's undefined?
Note that in order to get my app to work the way I intended it to work I relied on the default value argument to the preferences getter method as follows:
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean isVisible = sharedPrefs.getBoolean("prefVisible", true); // default = true
This leaves me a bit confused as to why there are 2 ways to control the default value of a preference: defining it in the Xml or providing the default value in the getBoolean method.
No the preferences can be set if you call PreferenceManager.setDefaultValues. So if you call this when the first time the app is launched then your view will be shown.
You can read more at http://developer.android.com/guide/topics/ui/settings.html

How to Initialize Preferences Screen on First Invocation?

I believe I am correctly initializing preferences from XML. My Preferences Screen also works properly and reflects the correct user selected settings.
However, upon first invocation of that Preferences Screen, none of the settings are checked (checkbox) or selected (list). This, of course, confuses the user as it doesn't reflect the current (default/initial) value.
Since all I do to invoke the Preferences Screen is
startActivity(new Intent(this, EditPreferences.class));
And my EditPreferences class only contains:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.usersettings);
}
I am not sure where or how to tell it to pre-initialize the visual display with the default setting.
I have this hunch that all I am missing is a single line somewhere, but I don't know where: XML file? override a method in EditPreferences? Other?
Can't you define the default value in the XML itself?
<CheckBoxPreference ...
android:defaultValue="true"
... />
You can specify a default value on a preference (in your xml layout for example):
<EditTextPreference android:defaultValue="whatever" />

how do i reference a checkbox in my options menu from my main activity in android?

i have an option to "mute" in my menu options like this:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference android:summary="Mute all sound effects" android:title="Mute"android:key="muteSound"></CheckBoxPreference>
</PreferenceScreen>
now how can i check if that checkbox is checked or not?
You have to get the SharedPreferences:
//in the main activity you should set the default values in case user has never entered the preferences screen
PreferenceManager.setDefaultValues(this, R.xml.preferences_file, false);
preferences = PreferenceManager.getDefaultSharedPreferences(this);
and then read the value:
preferences.getBoolean("muteSound", true) //the second argument is the default value
Use the following in an Activity:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean muteSound = prefs.getBoolean("muteSound", false);

Categories

Resources