I am trying to setup a new password using EditTextPreference. In this Android 4.0.3, clicking the EditTextPreference opens a dialog window where user can enter text. However, on pressing ok, it still shows the old text value and not the newly entered value.
public class MyPreferencesActivity extends PreferenceActivity {
EditTextPreference edp_password = (EditTextPreference) findPreference("pref_key_account_password");
edp_password.setOnPreferenceChangeListener(new OnPreferenceChangeListener(){
public boolean onPreferenceChange(Preference preference, Object newValue) {
String password = edp_password.getText();
Log.v(TAG, "Password is: " + password);
return true;
}
});
I spend a while trying to make it work, but couldn't find any good solution. How can I retrive the newly entered text after user presses Ok.
Try this way:
EditTextPreference pref_dayCount = (EditTextPreference)findPreference("pref_dayCount");
pref_dayCount.setDefaultValue(30);
pref_dayCount.setSummary(getResources().getString(R.string.pref_plan_days_number_summary)+" "+pref_dayCount.getText());
pref_dayCount.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
preference.setSummary(getResources().getString(R.string.pref_plan_days_number_summary)+" "+newValue.toString());
return true;
}
});
ok i think i know what the problem is, from the android developers references, it says that this callback is invoked before the internal state has been updated, so i guess that why you see the old value.
the way you did it was fine, but i'm guessing you need to use the newValue object, maybe something like this:
public class MyPreferencesActivity extends PreferenceActivity {
EditTextPreference edp_password = (EditTextPreference) findPreference("pref_key_account_password");
edp_password.setOnPreferenceChangeListener(new OnPreferenceChangeListener(){
public boolean onPreferenceChange(Preference preference, Object newValue) {
Log.v(TAG, "Password is: " + (EditText)newValue.getText());
return true;
}
});
i'm not sure if this works, i only free write it :P
password.getText will not get the updated value. Use:
String password = newValue.toString();
Related
Im trying to accomplish such a thing:
when I check CheckBoxPreference 'A' the other preference ('B') shows below the A,
when I uncheck 'A', preference 'B' hides...
So generally speaking it should work just like dependency but not only enabling/disabling th preference B, but hiding it.
This is what i came up with:
prefA = (CheckBoxPreference)findPreference("preference_A");
prefA.setChecked(false);
prefB = findPreference("preference_B");
category.removePreference(prefB);
prefA.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
boolean switchedOn = (Boolean)newValue;
if (switchedOn)
{
Log.d("pref_test", "prefA checked");
category.addPreference(prefB);
}
else
{
Log.d("pref_test", "prefA UNchecked");
prefB = findPreference("preference_B");
category.removePreference(prefB);
}
return switchedOn;
}
});
prefA and prefB have been defined earlier as PreferenceFragment class fields.
The problem is that it works fine only for 2 clicks and my logs say:
prefA checked
prefA UNchecked
prefA UNchecked
Like it was calling onPreferenceChangeListener twice for unchecking (obviously resulting in .removePreference(prefB) method returning null).
Any idea on solving the issue?
Would it not work doing something like this?
prefA = (CheckBoxPreference)findPreference("preference_A");
prefA.setChecked(false);
prefB = findPreference("preference_B");
category.removePreference(prefB);
prefA.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
boolean switchedOn = (Boolean)newValue;
if (switchedOn) {
Log.d("pref_test", "prefA checked");
category.addPreference(prefB);
} else {
Log.d("pref_test", "prefA UNchecked");
prefB = findPreference("preference_B");
category.removePreference(prefB);
}
return true;
}
});
I'm trying to prevent the user from entering an empty string into an EditTextPreference (in the example, catName). I use a OnPreferenceChangeListener to detect when a change is made to the EditTextPreference, and if there is a change and the string is blank, I use the EditTextPreference.setText() command to reset to the old value. However, the new value doesn't show up properly if I reopen the EditTextPreference in the GUI (the string is blank), and if I go back into the main app, I can verify that a blank value is being saved to the preferences.
I've verified that the if statement executes as expected, and that my parameter keeping track of the old name (oldCatName) is updating as expected. I can even log the catName.getText() value right before the setOnPreferenceChangeListener finishes execution and I always see the value I expect (the new value set by the user, and when they enter a blank value, it properly resets to the old value). I'm not sure why setting the value to the EditTextPreference isn't saving the value to the preferences file or updating the GUI.
public class SettingsActivity extends PreferenceActivity {
private String oldCatName;
private EditTextPreference catName;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
catName = (EditTextPreference) findPreference("cat_name");
oldCatName = catName.getText();
catName.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object newVal) {
final String value = (String) newVal;
if (value.equals("")) {
catName.setText(oldCatName);
Log.e("new value", catName.getText());
}
else
oldCatName = value;
return true;
}
});
}
}
Thanks for the help!
-Michael
Edit: A clarification. The logic in the if statement is executing correctly. The string value of the EditTextPreference is even updating correctly. However, the value in the GUI if the user taps on the EditTextPreference again does not correctly update, and the value in the app's shared preferences does not update correctly. It stays blank.
Finally found a solution by doing the following:
I used a SharedPreferences.OnSharedPreferenceChangeListener instead of a Preference.OnPreferenceChangeListener. The Preference.OnPreferenceChangeListener looks for when the user changes a preference through the settings menu, and behaves before the change is committed to the preference data. The SharedPreferences.OnSharedPreferenceChangeListener listens for changes to the actual preference data, not changes in the GUI, so it happens a little later. I noticed that in my first attempt, I could run setText() immediately followed by getText() on my EditTextPreference object, and the getText() value wouldn't match what I had just set the setText() value to. I'm not sure why this happens, but waiting for the changes to actually commit before trying to run setText() led to the correct response. Maybe it was a timing issue?
I run setPreferenceScreen(null) and addPreferencesFromResource(R.xml.settings) after updating the value in the EditTextPreference. If I didn't do this, sometimes when the user would click on the EditTextPreference again, the value in the field would appear blank even though the value in the settings file wasn't. This forces the settings page to, more or less, refresh itself.
The working code is below:
public class SettingsActivity extends PreferenceActivity {
private String oldCatName;
private EditTextPreference catName;
private SharedPreferences.OnSharedPreferenceChangeListener listener;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
createListener();
catName = (EditTextPreference) findPreference("cat_name");
oldCatName = catName.getText();
}
private void createListener() {
listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
#Override
public void onSharedPreferenceChanged(
SharedPreferences sharedPreferences, String key) {
String value = sharedPreferences.getString("cat_name", "NULL");
if (value.equals("")) {
catName.setText(oldCatName);
setPreferenceScreen(null);
addPreferencesFromResource(R.xml.settings);
} else {
oldCatName = value;
}
}
};
PreferenceManager.getDefaultSharedPreferences(getApplicationContext())
.registerOnSharedPreferenceChangeListener(listener);
}
}
~9 yrs late, Hope this helps some one else.
I too faced similar issue. It can be done simply by returning false. As the documentation states returning true will update the value and you want to ignore changes.
catName.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object newVal) {
final String value = (String) newVal;
if (value.equals("")) {
catName.setText(oldCatName);
Log.e("new value", catName.getText());
return false ; // <----------------------------------
}
else
oldCatName = value;
return true;
}
});
In Android 4, I have a preference value that I want to appear in the standard form E2C56DB5-DFFB-48D2-B060-D0F5A71096E0. But the user may enter the value without dashes. I would like to allow the user to enter it with any combination of whitespace or dashes, and simply have my code normalize it.
I am using the code below, and I see the log line, but it does nothing. I am guessing this is because Android has another Editor object open that overwrites my changes. Is there any other way to accomplish this?
public class UuidFragment extends PreferenceFragment {
...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
this.getPreferenceScreen().findPreference("pref_uuid").setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference,
Object newValue) {
if (newValue.toString().length() != 0) {
String normalizedUuid=normalizeUuid(newValue.toString());
// TODO: this code runs but does nothing, I think because after committing the change, there is a higher level editor that commits the old value
// thereby undoing this change
if (!normalizedUuid.equals(newValue.toString())) {
Log.d(TAG, "Adjusting uuid from "+newValue.toString()+" to "+normalizedUuid);
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(UuidFragment.this.getActivity());
SharedPreferences.Editor editor = settings.edit();
editor.putString(preference.getKey(), normalizedUuid);
editor.commit();
}
return true;
}
}
});
}
}
Try subclassing EditTextPreference and overriding setText(). In your setText() method, fix up the passed-in string before chaining to the superclass. Then, reference your EditTextPreference subclass from your preference XML.
I have an EditTextPreference (key "userfirstname"). I want to call method when it has been changed. I read documentation of PreferenceActivity and EditTextPreference, but I didn't find relevant solution.
Is there any way to do that ?
Preference userNamePref = findPreference("userfirstname");
userNamePref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference,
Object newValue) {
//do something
return true;
}
});
I am using Edittextpreference as one of the preference in settings section. I want to validate this edittextpreference when the user enters data to it and clicks ok; before saving it in sharedpreference.
I am trying to do something like this but this saves the preference first I suppose.
editTextPreference
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference,
Object newValue) {
if (((newValue.toString().length() == 15) {
// save preference only if length is equal to 15
}
})
});
can someone guide me how to validate edittextpreference before it is saved in sharedpreference so that I can decide if I want to save it or not.
According to doc here
Called when a Preference has been changed by the user. This is called
before the state of the Preference is about to be updated and before
the state is persisted.
And it returns True to update the state of the Preference with the new
value.
So you can do the following
editTextPreference
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference,
Object newValue) {
if (((newValue.toString().length() == 15) {
//
return true;
}
else{
// invalid you can show invalid message
return false;
}
})
});