I'ma a bit confused here. I'm trying to change the value of an EditTextPreference, but it is not updated in the view. (This is in a PreferenceActivity)
Here is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.modify_instrument_preferences);
// Set default values
SharedPreferences customSharedPreference = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = customSharedPreference.edit();
modifying = getObjectWithName(); //Some object with a name;
editor.putString("namePref", modifying.getName());
editor.commit();
android.util.Log.d("TEST", "written: "+customSharedPreference.getString("namePref",""));
}
My printlns print out valid information, and the commit() returns true, but on clicking the EditTextPreference, it displays the old value. If I rotate the screen, causing the onCreate to get run again, the EditTextPreference has the right value.
So perplexing. Why isn't this change being updated in the UI?
Edit:
I'm not sure why the above isn't working, but I managed to change it just by doing:
EditTextPreference namePref = (EditTextPreference) findPreference("namePref");
namePref.setText("the text");
That updated the view everytime.
Although I know there are some constructs in place for PreferenceActivities to keep track of this info themselves, it doesn't seem to be well documented. I have found that adding an onPreferenceChangeListener to the preference will allow you to make those edits as soon as the preference is changed.
Related
Order of calls according to logcat is onCreate, setViewValues, setStrikethroughFlag, (ROTATE), onCreate, setViewValues:
SharedPreferences mSettings;
Editor spEditor;
#Override
public void onCreate(Bundle savedInstanceState) {
....
mSettings = getSharedPreferences("prefs", "")
spEditor = mSettings.edit();
setViewValues();
}
public void setViewValues() {
boolean isStrikeThru = mSettings.getBoolean(STRIKETHROUGH, false);
Log.d("TRACE", "setViewValues, strikethrough " + isStrikeThru);
}
public void setStrikethroughFlag() {
spEditor.putBoolean(STRIKETHROUGH, true);
spEditor.commit();
}
The logcat says setStrikethroughFlag() is called. Then I rotate the screen, onCreate and setViewValues are called. In setViewValues, I thought it would recognize the saved value of STRIKETHROUGH, true. But the logcat trace says the value of isStrikeThru is false.
Try calling the setViewValues() from the Constructor. In Screen orientation change, the oncreate of the activity will gets called again and resets your values.
From know i can see, that you creating local variable in your onCreate()
And in your setViewValues() you accessing your class field variable.
So i am not sure that you are calling getSharedPreferences() on your field variable.
The same is about your spEditor. Fix this and try one more time.
If this will not help you, give as the rest of your code.
UPDATE
Try this mSettings = getSharedPreferences("prefs", Context.MODE_PRIVATE);
The mistake was using quotes around the key when storing my shared preference. The keys didnt match so nothing was persisted, sorry.
I have in my main activity access to a shared preference like this:
preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("maxValue", "666");
Boolean test = editor.commit();
when I open my Preference activity I see for that preference the value 666. So that works without issue.
Now I want to do pretty much the same in my Preferences.java, background is that I want to check the input string and notify the user if something is wrong, e.g. I want to make sure that if the users sets the maxValue above 1000, to tell him that this is too high and set it to 899 automatically. I have implemented a change listener:
#Override
public boolean onPreferenceChange(Preference pref, Object newValue)
{
if(Integer.valueOf(etAdd.getEditText().getText().toString()) > 899)
{
SharedPreferences.Editor editor = preferences.edit();
editor.putString("maxValue", "899");
Boolean test = editor.commit(); //test shows true
Toast.makeText(Preferences.this, "value too high have set it to the maximum of 899", Toast.LENGTH_LONG).show(); //this toast is shown
}
return true;
}
When I re-open this preference, even after leaving the preference activity and returning back to it, I see the value the user has entered, e.g. 1000.
anyone an idea?
thanks.
According to the documentation onPreferenceChange is called BEFORE the changes are written. If you return true (accept new Value), after leaving onPreferenceChange the newValue object is successfully applied and your modifications are ignored. That is the reason of mentioned behavior. Returning false would do the trick.
To project your corrected value to PreferenceActivity immediately, use SetText on your EditTextPreference.
Using typecasted newValue.toString() instead of using contents of the corresponding EditText is also worth considering.
I have a PreferenceActivty in my Android app, which due to compatibility reasons I use via the getPreferenceScreen() method and some Preference objects which I create in code, mostly CheckBoxPreference and SwitchPreference.
Up to the previous version of my app there were 8 preferences in total and everything worked fine, but now I added 2 more preferences and I'm experiencing a REALLY weird issue.
The second preference on the screen is a SwitchPreference. When I open the activity, it is checked. If I scroll down the screen without actually changing anything, suddenly its value is automatically set to OFF. I tried adding an OnChangeListener to the Preference and implementing OnSharedPreferenceChangeListener, but the results are the same: once that particular Preference disappears from the screen, it is turned OFF. If it's set to OFF, it keeps its value and the change listener is not called.
Does anyone have any idea as to why could this be happening? I'm completely lost...
Thanks in advance!
The code for my preferences is basically this, repeated 5 times for 5 different settings, on the onCreate method:
controlWifiPreference = new CheckBoxPreference(this);
controlWifiPreference.setKey(Constants.PREF_1_KEY);
getPreferenceScreen().addPreference(controlWifiPreference);
wifiPreference = new SwitchPreference(this);
wifiPreference.setKey(Constants.PREF_2_KEY);
getPreferenceScreen().addPreference(wifiPreference);
Since the preferences are inside a TabActivity, on the onResume method I call setChecked() for every preference to set its value again, though I'm not sure that it's completely neccessary.
And, finally, I have an onSharedPreferenceChanged method that activates/deactivates preferences when others are clicked, because I couldn't get the setDependency method to work. It's something like this (again, repeated five times):
if (key.equals(controlWifiPreference.getKey())) {
wifiPreference.setEnabled(controlWifiPreference.isChecked());
}
Turns out it was an Android bug in the SwitchPreference class. Someone (who I'm VERY thankful to ;)) reported it to b.android.com and even posted a workaround. It's all here: https://code.google.com/p/android/issues/detail?id=26194
How you implemented preferences inside TabActivity?I checked your code in my own IDE inside a PreferenceActivity and its working like a charm.If you need to have some pseudo prefences inside your activity, you should not use prefernces and instead, you will need to use normal forms items and save their values to the preferences manually.here is the code i tested and its working ok:
public class PreferencesFromCode extends PreferenceActivity implements
OnSharedPreferenceChangeListener {
private SwitchPreference switchPref;
private CheckBoxPreference checkboxPref;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setPreferenceScreen(createPreferenceHierarchy());
}
private PreferenceScreen createPreferenceHierarchy() {
// Root
#SuppressWarnings("deprecation")
PreferenceScreen root = getPreferenceManager().createPreferenceScreen(
this);
// Inline preferences
PreferenceCategory inlinePrefCat = new PreferenceCategory(this);
inlinePrefCat.setTitle(R.string.inline_preferences);
root.addPreference(inlinePrefCat);
// Checkbox preference
checkboxPref = new CheckBoxPreference(this);
checkboxPref.setKey("checkbox_preference");
checkboxPref.setTitle(R.string.title_checkbox_preference);
checkboxPref.setSummary(R.string.summary_checkbox_preference);
inlinePrefCat.addPreference(checkboxPref);
// Switch preference
switchPref = new SwitchPreference(this);
switchPref.setKey("switch_preference");
switchPref.setTitle(R.string.title_switch_preference);
switchPref.setSummary(R.string.summary_switch_preference);
inlinePrefCat.addPreference(switchPref);
/*
* The Preferences screenPref serves as a screen break (similar to page
* break in word processing). Like for other preference types, we assign
* a key here so that it is able to save and restore its instance state.
*/
// Screen preference
PreferenceScreen screenPref = getPreferenceManager()
.createPreferenceScreen(this);
screenPref.setKey("screen_preference");
screenPref.setTitle(R.string.title_screen_preference);
screenPref.setSummary(R.string.summary_screen_preference);
return root;
}
#Override
protected void onResume() {
super.onResume();
PreferenceManager.getDefaultSharedPreferences(this)
.registerOnSharedPreferenceChangeListener(this);
/*
* getPreferenceScreen().getSharedPreferences()
* .registerOnSharedPreferenceChangeListener(this);
*/
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
Log.i("ghjg", "changed key is : " + key);
if (key.equals(checkboxPref.getKey())) {
switchPref.setEnabled(checkboxPref.isChecked());
}
}
}
However you may override onContentChanged() and see what happens.
I'm use Android ICS SDK and I would like to do a PreferenceScreen which use MultiSelectListPreference (avalaible for API Level 11&+.
I just want to persist the value in SharedPref, refresh the summary of the MultiSelectListPreference and refresh the dialog list.
Here's my code :
Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.newgame);
mMultiCharacters.setOnPreferenceChangeListener(this);
}
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor settingsEditor = settings.edit();
settingsEditor.putStringSet( preference.getKey() , (Set<String>) newValue);
settingsEditor.commit();
//display new summary
initChar();
return false;
}
XML part :
<MultiSelectListPreference
android:entries="#array/characterNames"
android:entryValues="#array/characterNames"
android:key="pref_characters"
android:persistent="true"
android:title="Chars :" />
The behavior is quite strange. The dialog list doesn't refresh… some ideas ? Thanks!
Well.. I used MultiSelectListPreference once, and here is what I "think" ..
You are returning false in the onPreferenceChange listener, which I think it doesn't only NOT commit the changes, but also may be reverting to the previous values. I'm aware that you are committing the changes through settingsEditor but may be the false return is reverting back the old values, so I suggest returning true.
One more thing, the newValue object is actually a HashSet which contains the newly selected values, so you can use it to do whatever you want but you don't have to use it to commit changes if you are returning true.
Good Luck.
I use a Preference in a PreferenceActivity to load default values: when this specific Preference is clicked, something like this happens:
private String mResetKeys = "key1,key2,key3";
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
SharedPreferences.Editor prefs_editor = prefs.edit();
for (String current_pref : mResetKeys.split(",")) {
prefs_editor.remove(current_pref);
}
prefs_editor.commit();
But afterwards, the Preferences whose corresponding SharedPreference was reset still show the old value - it seems to be cached in the Preference. Only when I leave the PreferenceActivity and reopen it, the Preferences show the new values.
How can I update the PreferenceActivity programmatically?
I had a similar problem. This probably isn't the most correct fix but it worked for my purposes. Right after I did the commits, I called the Activity.recreate(); method.
The activity will restart (onDestroy()/onCreate()/etc) but for my purposes all I needed was a special handling on one preference. I listened for a certain preference with an OnPreferenceClickListener and made an alert dialog box with a kind of warning message and an option to change their mind. If they did want to change their mind, I did my commit of the new value to the preference activity and then called recreate() so that the checkbox preference would be updated.
However, I am also interested in a way to do this without recreating the activity...
Update preference value without reloading PreferenceActivity
from http://liquidlabs.ca/2011/08/25/update-preference-value-without-reloading-preferenceactivity/
Here is how to update default shared preference value of target element (in this case EditTextPreference)
public class YourCustomPreference extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
// some logic goes above, when you want to reset value and update EditTextPreference value
// For convenience, I am going to wrap two different task in different methods
private void resetPreferenceValue() {
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
SharedPreferences.Editor prefEditor = sharedPref.edit(); // Get preference in editor mode
prefEditor.putString("your_edit_text_pref_key", "DEFAULT-VALUE"); // set your default value here (could be empty as well)
prefEditor.commit(); // finally save changes
// Now we have updated shared preference value, but in activity it still hold the old value
this.resetElementValue();
}
private void resetElementValue() {
// First get reference to edit-text view elements
EditTextPreference myPrefText = (EditTextPreference) super.findPreference("your_edit_text_pref_key");
// Now, manually update it's value to default/empty
myPrefText.setText("DEFAULT-VALUE"); // Now, if you click on the item, you'll see the value you've just set here
}
}