ateiob
ateiob

Reputation: 9106

How to get notified on CheckBoxPreference state change?

I need to set/unset listener dynamically in response to a Preference CheckBox user setting change.

One approach I have seen is to use onSharedPreferenceChanged() and check for that checkbox's key.

But somehow this looks to me inefficient. I was thinking more in the direction of setting some kind of listener on the class derived from PreferenceActivity. Perhaps onContentChanged()?

Which approach would you recommend and why?

Can you point to a sample working code?

Upvotes: 3

Views: 1691

Answers (2)

slybloty
slybloty

Reputation: 6506

You should implement OnSharedPreferenceChangeListener. And then set up a listener to catch key changes:

getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);

The you should be able to retrieve the checked changes:

public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,String key) 
{

}

Upvotes: 4

Jason L
Jason L

Reputation: 1822

Here's the documentation. Using the XML file, you can set a method to be executed when the widget is clicked. Setting android:onClick = foo will execute the method foo when the widget is clicked, passing it the parameter View v, v being the View that was clicked.

Upvotes: 0

Related Questions