CheckboxPreference onClick()

8.1k views Asked by At

I want to implement a 2 part preference screen. If checkbox is clicked, first category should lock and 2nd one unlock. If it's not, reverse. Now I see it only works if I go to previous activity and then to new (sharedPreferences). What listener should I override and how?

2

There are 2 answers

0
Maaalte On BEST ANSWER

You could try something like:

final Preference otherpref = (Preference) findPreference("otherpref"); 
final Preference pref = (Preference) findPreference("checkbox");        
pPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {

    public boolean onPreferenceClick(Preference preference) {
        otherPref.setSelectable(false);
        Toast.makeText(getBaseContext(), "Some text", Toast.LENGTH_SHORT).show();
        return true; 
    }
});

And disable your desired categories.

0
Eric Nordvik On

I have implemented my preference activity as a class which extends PreferenceActivity and implements OnSharedPreferenceChangeListener

public class SettingsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    Context context = getApplicationContext();
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
    settings.registerOnSharedPreferenceChangeListener(this);
  }

  @Override
  public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    // Check the checkboxes
  }
}