I'm trying to gain access to a (custom) list preference and change its selected value (i.e. if index 3 is selected, change the selection to index 1). However, findPreference() can only be used inside a PreferenceActivity. I need to access the preference and change its selection inside a regular Activity. Is there a way this can be done? I don't see anything in SharedPreferences that I can use to change the selection, only a list preferences value.
This is how you do it,
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreference(this);
prefs.getString(PREF_KEY, "default");
Related
I am trying to create a time selection preference by extending the DialogPreference. I have specified a android:defaultValue in the Preference XML. Everything works well, but when I try to read the value of the preference from code, I get an empty string. Ideally, if the preference is not set, I should get the value as 5:00 which is mentioned asandroid:defaultValue in the Preference XML.
When I retrieve the preference, how can I get the value specified as android:defaultValue in the
Preference XML, if user has not yet set the preference?
<com.sample.package.TimeDialogPreference
android:defaultValue="5:00"
android:key="#string/pref_key_time"
android:title="#string/pref_title_time"
android:summary="#string/pref_summary_time"
android:dialogLayout="#layout/preference_time"
/>
I have 2 fragments.
Fragment 1
Loads sharedpreference to display string
Fragment 2
Saves sharedprefence for string
Is it possible to retrieve that string in my first Fragment without running the second Fragment?
Yes, this is possible. You just need to make sure you are reading with the same key you used to write with:
SharedPreferences prefs = getSharedPreferences("MyPrefs", MODE_PRIVATE);
// Reading from SharedPreferences
String value = prefs.getString("myKey", "defaultValue");
Log.d(LOG_TAG, value);
Note that we've assigned a defaultValue as the return value here. If there is no value with the key "myKey" in your shared prefs, it will instead return "defaultValue". This is a nice safeguard, think of it like a null pointer check - you will always get a value from getString(), even if it's just the default.
You don't need to be in the same activity for this to work, you just need to make sure that 1) your preferences name is the same and 2) the key used to store the value is the same in both spots.
First, don't get confuse between Activity and Fragment.
And yes, you can.
Here are my questions:
How can I get the value of a switch preference?
Are things inside a preference fragment already a shared preference?
If so, how can I access shared preferences from a preference fragment?
1. To get the value of a switch:
CompoundButton cb = (CompoundButton)view.findViewById(R.id.myswitch);
if(cb.isChecked())
cb.setChecked(false);
else
cb.setChecked(true);
Reference: Toggle Buttons
2. Yes, the elements in a PreferenceFragment are automatically stored.
3. To get the SharedPreference object of a PreferenceFragment, use:
getDefaultSharedPreferences(Context context)
This is an amazing example on how to make a PreferenceFragment:
http://android-er.blogspot.hu/2012/07/example-of-using-preferencefragment.html
Basically the way it works is that you define the preferences with their types and keys. The preference fragment stores its data in the SharedPreferences, which you obtain via Android getDefaultSharedPreferences , and edit its values and obtain them from an Editor.
In a Preference Fragment, you automatically have the data of the prefs linked between the Fragment and the SharedPrefs.
Also look at this example code: http://www.mysamplecode.com/2011/11/android-shared-preferences-example_12.html
When the user updates a shared preference through a subclass of PreferenceActivity I check whether or not the new value is valid at the given time. If not the value should be changed back in the onSharedPreferenceChanged method.
This works so far. I set an OnSharedPreferenceChangedListener, the method gets called. The user-set value will be overwritten and the new value will be used in the app, however when I open this specific preferences value (in this case a ListPreference) again the wrong list item will be selected (the one the user selected, not the one set in the Listener). I tried overwriting the value with both:
mPrefs.edit().putString("answers", value.toString()).commit();
mPrefs.edit().putString("answers", value.toString()).apply();
Are there additional steps I need to take to update the ListPreference? After restarting the PreferenceActivity the value will be displayed correctly.
try to use Override method SharedPreferenceChanged
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
if (key.equals(KEY)) {
Preference ServicePref = findPreference(key);
// Set summary to be the user-description for the selected value
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
}
}
I found a way to solve this issue by setting the value manually as described here:
http://liquidlabs.ca/2011/08/25/update-preference-value-without-reloading-preferenceactivity/
I have a preference settings for which i have configured a dialog box pop up on click of preference using the below code.
Dialog passwordDialog = new Dialog(this);
passwordDialog.setContentView(R.layout.password_dialog);
passwordDialog.setTitle("Set new password.");
passwordDialog.setCancelable(true);
passwordDialog.show();
The dialog is coming fine. However In the dialog i have a password edit text and a confirm password edit text. If these matches with each other i need to save the password to preference. I do not know how to save the value to a preference on ok click in my custom dialog. Please let me know how to do this. Thank you for your time and help.
You can retrieve the preferences, and then commit information to it :
SharedPreferences preferences = getPreferenceManager().getSharedPreferences();
preferences.edit().putString("passwordKey", editText.getText().toString()).commit();
To add information to a preference, start by calling edit() (returns an editor), add the value you want (key/value, like a Map), and never forget to call commit() (to commit the changes).
You can then access your value using
preferences.getString("passwordKey", defaultValue);