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();
Related
i have kept background music for my android app by default.I have used preference for closing the music when the user wants.Now i want to check in an activity whether the checkbox is checked or not..How can i access the preference checkbox from any other activity..How can i implement it?
You can use this:
SharedPreferences preferences = getSharedPreferences("pref",
Context.MODE_PRIVATE);
boolean x = preferences.getBoolean("mycheckbox",false);
"mycheckbox" is the key that you have used when creating preference check box.
<CheckBoxPreference
android:key="mycheckbox"/>
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.
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);
I have a bit of confusion about setting default values of shared preferences... for example, I have a PreferenceActivity with two or more PreferenceFragment and each of the preference fragmens has its own *preference_fragmentX.xml* file used to build the preference view. When I set the default values with
PreferenceManager.setDefaultValues(getActivity(), R.xml.?????, false);
Which XML should I specify? Should I build another XML preference file containing all the preferences of the *preference_fragmentX.xml* files with the default values and use this one in setDefaultValues? For example, I may build an app that registers for a broadcast event and I'm not sure the user opens the preference activity before the broadcast event is fired for the first time, so I'd like to initialize default preferences in the broadcast event, or in another activity started by it.. so, how do I initialize all the preferences of all the fragment panes?
It works if I build a default_preferences.xml file which contains all the preference keys defined in all the preference fragments XML files (I specify only the a PreferenceScreen root tag and all the preference tags under it, without specifying all the other tags like PreferenceCategory.. furthermore for each preference I specify only the key and the default vale attributes), but I don't know if it is the correct way to do this task, since I must replicate part of the other XML files:
preferences_fragment1.xml:
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="#string/my_preference_category">
<RingtonePreference
android:key="preference_ringtone"
android:ringtoneType="alarm|notification"
android:showSilent="true"
android:summary="#string/preference_ringtone_summary"
android:title="#string/preference_ringtone_title" />
<ListPreference
android:key="preference_list"
android:entries="#array/list_items"
android:entryValues="#array/list_values"
android:summary="#string/preference_list_summary"
android:title="#string/preference_list_title" />
</PreferenceCategory>
</PreferenceScreen>
default_preference.xml:
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<RingtonePreference
android:defaultValue="content://settings/system/notification_sound"
android:key="preference_ringtone"/>
<ListPreference
android:defaultValue="5"
android:key="preference_list"/>
</PreferenceScreen>
I am trying to implement user preferences. A problem I have is that if one of my check boxes is ticked I want to set the other to false. I know about dependency and from what I have seen it doesn't change the value. Just disables you from changing the preference. What can I do to change a checkbox to false if another is true? This is my xml:
<PreferenceCategory android:title="Launch Settings">
<CheckBoxPreference
android:key="enable_nearest_station"
android:title="Nearest station"
android:defaultValue="false"
android:summary="Will launch your nearest station on startup"
android:disableDependentsState="true"/>
<CheckBoxPreference
android:key="enable_default_station"
android:title="Default station"
android:defaultValue="false"
android:dependency="enable_nearest_station"
android:summary="Allows you to choose a default to launch with."
/>
/>
</PreferenceCategory>
Thanks in advance.
Use the set setOnPreferenceChangeListener(Preference.OnPreferenceChangeListener onPreferenceChangeListener) to your CheckBoxPreference and code to set false other CheckBoxPreference.