OnPreferenceChangeListener for every setting - android

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.

Related

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

PreferenceActivity - EditTextPreference - on value changed

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

getting boolean from shared preference always returns false

I store my boolean like this in my preference activity:
OnPreferenceChangeListener locaListener = new OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object newValue) {
SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
if(newValue.toString().trim().equals("true")){
editor.putBoolean("locationEnabled", true);
}else if (newValue.toString().trim().equals("false")){
editor.putBoolean("locationEnabled", false);
}
editor.commit();
return true;
}
};
and try to retrieve it like this in my main activity:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean prefLocationEnabled = prefs.getBoolean("locationEnabled", false);
Same approach with string works perfect but with boolean it seems to return always the default value which is false. Anyone knows what am I doing wrong?
Once you call getPreferences(MODE_PRIVATE) when you save and when you load you call PreferenceManager.getDefaultSharedPreferences(this);
I think it's not same preferences. Use only one of these
Try this code. if you give the parameter like true or false it will Return the value you give.
Boolean tmp = null;
tmp = sharedPreferences.getBoolean("key", tmp.getBoolean(""));
It gives you the value you get.

Getting value from custom SeekBarPreference

I found this code on internet and thought I would give it a try. I put it in my preferences and it was exaclt the way I wanted it to be but my only problem is that I cant get the value of SeekBar.
Here is the link to code that I am using in my preference:
http://android.hlidskialf.com/blog/code/android-seekbar-preference
and here is the part of preference xml:
<com.mypack.SeekBarPreference android:key="zoom"
android:title="Zoom"
android:summary=""
android:dialogMessage="Zoom level"
android:defaultValue="50"
android:text=" %"
android:max="100"
/>
can anyone tell me how to get value of seekbar after user has changed it in preferences window?
You could use setOnPreferenceChangeListener():
SeekBarPreference mSeekBarPreference = new SeekBarPreference(this, null);
mSeekBarPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
// TODO Auto-generated method stub
int value = mSeekBarPreference.getProgress();
// Or you just cast the newValue Object
return true;
}
});
Should be just like any other preference.
implement SharedPreferences.OnSharedPreferenceChangeListener in your activity, then:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences( YourActivity.this );
prefs.registerOnSharedPreferenceChangeListener( this );
-
public void onSharedPreferenceChanged( SharedPreferences prefs, String key ) {
mZoomValue = prefs.getInt( "zoom", 50 );
}

RingtonePreference not firing onSharedPreferenceChanged

My Preferences all trigger the onSharedPreferenceChanged event upon a change. It works for all preferences: Checkbox, List, custom, etc. But it won't be called if I select a ringtone from the RingtonePreference. I have this code:
<CheckBoxPreference android:title="#string/pref_notification"
android:defaultValue="true" android:summary="#string/pref_notification_summary"
android:key="prefNotification" />
<RingtonePreference android:title="#string/pref_ringtone"
android:key="prefRingtone"
android:summary="#string/pref_ringtone_summary" android:ringtoneType="all" />
<CheckBoxPreference android:title="#string/pref_vibrate"
android:defaultValue="true" android:summary="#string/pref_vibrate_summary"
android:key="prefVibrationOn" />
<ListPreference android:title="#string/pref_notification_interval"
android:summary="#string/pref_notification_interval_summary"
android:key="prefNotificationInterval" android:defaultValue="60"
android:entries="#array/prefs_interval" android:entryValues="#array/prefs_interval_values" />
And my class:
public class TimePrefsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
addPreferencesFromResource(R.layout.preferences);
Preference dbPref = (Preference) findPreference("prefDeleteDb");
dbPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference)
{
showWipeDbDialog();
return true;
}
});
}
#Override
public void onResume() {
super.onResume();
prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.registerOnSharedPreferenceChangeListener(this);
toggleEnableList();
}
#Override
public void onPause() {
prefs.unregisterOnSharedPreferenceChangeListener(this);
super.onPause();
}
#Override
public void onSharedPreferenceChanged(SharedPreferences arg0, String arg1)
{
boolean enabled = toggleEnableList();
if (enabled)
{
OnBootReceiver.setAlarm(TimePrefsActivity.this);
}
else
{
OnBootReceiver.cancelAlarm(TimePrefsActivity.this);
}
}
}
All the preferences, except the RingtonePreference, reach method onSharedPreferenceChanged. Does anyone have an idea? Thanks.
I struggled with the same problem which seems to be a bug in the android system.
After debugging the code I noticed the listener is not added to our RingtonePreference listeners map, unlike other classes like ListPreference.
I opened a ticket, but for now I found a way to overcome it using OnPreferenceChangeListener.
My code sets the preference summary to the selected ringtone, you can use your logic instead.
First make your activity implement OnPreferenceChangeListener and write the onPreferenceChange method
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
updateRingtoneSummary((RingtonePreference) preference, Uri.parse((String) newValue));
return true;
}
private void updateRingtoneSummary(RingtonePreference preference, Uri ringtoneUri) {
Ringtone ringtone = RingtoneManager.getRingtone(this, ringtoneUri);
if (ringtone != null)
preference.setSummary(ringtone.getTitle(this));
else
preference.setSummary("Silent");
}
Notice that unlike onSharedPreferenceChanged, onPreferenceChange is called before the preference is updated, so you must use the newValue parameter to get the selected data instead of getting it from the preference.
Then, set the listener on OnResume:
#Override
protected void onResume() {
super.onResume();
// A patch to overcome OnSharedPreferenceChange not being called by RingtonePreference bug
RingtonePreference pref = (RingtonePreference) findPreference(getString(R.string.pref_ringtone));
pref.setOnPreferenceChangeListener(this);
}
Hope this helps.
I also thought this was a bug in the system at first, but the issue is actually more subtle. RingtonePreference launches a new activity via an intent. This means that your PreferenceActivity gets paused. And you're unregistering your listener in onPause(). If you don't do this, it'll work fine (at least it did for me).
But naturally, you can't have your handler stay registered forever. I compromised by using onStart()/onStop() instead of onResume()/onPause().
I had to manually set the OnPreferenceChangeListener myself on my SettingsFragment:
Preference notificationSoundPref = findPreference(Constants.PREFS_NOTIFICATION_SOUND);
notificationSoundPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
// do what you need here
return true;
}
});

Categories

Resources