Reputation: 31
I have to have many checkboxpreference in preferncescreen and have to register each checkbox value changed and do some task based on if its on or off.How do I do this in checkboxpreference? I know to do this in checkbox with the below code:
public void onCheckboxClicked(View view) {
// Is the view now checked?
boolean checked = ((CheckBox) view).isChecked();
// Check which checkbox was clicked
switch(view.getId()) {
case R.id.Activate:
if (checked){
// Put some meat on the sandwich
Toast.makeText(Daydream.this, "checked!", Toast.LENGTH_LONG).show();
}else
// Remove the meat
Toast.makeText(Daydream.this, "UNchecked!", Toast.LENGTH_LONG).show();
break;
case R.id.sencond_id:
if (checked){
// Cheese me
}else
// I'm lactose intolerant
break;
// TODO: Veggie sandwich
}
}
But I dont know to do this with checkboxpreference.Is there any way to do like above?I know that the preferencescreen is deprecated but I have to use it because of simplicity to make two lines of listview.I hope I get a reply soon.Thanks
Upvotes: 1
Views: 2568
Reputation: 3695
use it something like this
final Preference otherpref = (Preference) findPreference("otherpref");
final Preference pref = (Preference) findPreference("checkbox");
pPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
Toast.makeText(getBaseContext(), "Some text", Toast.LENGTH_SHORT).show();
return true;
}
});
otherpref .setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
Toast.makeText(getBaseContext(), "Other text", Toast.LENGTH_SHORT).show();
return true;
}
});
Upvotes: 5