PreferenceActivity - EditTextPreference - on value changed - android

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;
}
});

Related

SwitchPreference stop working if setOnPreferenceChangeListener is implemented in PreferenceFragment

I've implemented SwitchPreference in PreferenceFragment and it works like a champ. But when I implement setOnPreferenceChangeListener on it its state does not change on the click event. Here is my implementation. Please have a look.
track_location = (CheckBoxPreference) findPreference("location_tracking");
track_location.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
return false;
}
});
if I remove the listener line, it works perfectly fine.
You have to return true inside onPreferenceChange

Android: Show/hide preference on checking/unchecking other CheckBoxPreference

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;
}
});

How to get updated text value from EditTextPreference - it gets old value

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();

Android EditText preference validation

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;
}
})
});

OnPreferenceChangeListener for every setting

I know that I can do something like this:
Preference pref = findPreference(getString(R.string.pref_vibrate_on_key));
pref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference,
Object newValue) {
LogUtil.d("Working!");
return true;
}
});
But I would like to add a Listener to every preference.
I tried doing:
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
sp.registerOnSharedPreferenceChangeListener(new OnSharedPreferenceChangeListener() {
#Override
public void onSharedPreferenceChanged(
SharedPreferences sharedPreferences, String key) {
LogUtil.d("working!");
}
});
but it doesn't work.
Is this possible? If so, what am I doing wrong?
Assuming you want the same listener called each time, this might be a simpler solution:
Preference.OnPreferenceChangeListener changeListener = new Preference.OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object newValue) {
// Code goes here
return true;
}
};
EditTextPreference pref = (EditTextPreference)findPreference(getString(R.string.pref1));
pref1.setOnPreferenceChangeListener(changeListener);
EditTextPreference pref2 = (EditTextPreference)findPreference(getString(R.string.pref2));
pref2.setOnPreferenceChangeListener(changeListener);
I think that onSharedPrefererenceChanged is fired upon saving the preference (when you click BACK or HOME in PreferenceActivity). I think that the easiest way is to create single class implementing OnPreferenceChangeListener and switch through Preference.getKey(); and set it as OnPreferenceChangeListener for each Preference.

Categories

Resources