How to enable a preference in my android application when other preference is disabled?
Asked Answered
R

4

23

I have used PreferenceActivity to have preference in my android application. I want one preference say "pref 2" to be enabled when other preference say "pref 1" is NOT checked and "pref 2" to be disabled when "pref 1" is checked. i.e. exactly opposite of the android:dependancy attribute.

How can I do that?

Raymonraymond answered 20/4, 2010 at 6:8 Comment(0)
P
9

I don't think there's any out-of-the-box solution for it, i.e. an inverted dependancy attribute. But there's always the click-listener:

preference1.setOnPreferenceClickListener(pref1_click);

....

private OnPreferenceClickListener pref1_click = new OnPreferenceClickListener() {
    public boolean onPreferenceClick(Preference preference) {
        preference2.setEnabled(!preference1.isChecked());
        return true;
    }
}
Pentangular answered 20/4, 2010 at 6:15 Comment(0)
S
97

Yes it's possible to do this out of the box. Let's say you want to disable pref2 when pref1 is off
Here's the code(preference xml layout) to put in for pref1:

<CheckBoxPreference
    android:title="Pref1"
    android:key="pref1">
</CheckBoxPreference>

Here's the code(preference xml layout) to put in for pref2:

<EditTextPreference
    android:dialogMessage="Pref 2 dialog"
    android:title="Pref2"
    android:key="pref2" 
    android:dependency="pref1">
</EditTextPreference>

Like sigmazero13 said, by default disableDependentsState is false so you don't need to include it in the pref1 attributes.

Subkingdom answered 23/6, 2011 at 15:3 Comment(3)
For me it was better to do this sort of thing in XMLSeagrave
What if these preferences are on different xml layouts?Internuncial
@Internuncial I use strings (in the strings.xml file) as my key names. This way, you can reference the key in your code, and other xml files. That should work.Barrie
F
31

According to the docs here, you can add an attribute to the CheckBoxPreference tag like so:

android:disableDependentsState="true"

By default this is false, which means that the dependents are disabled when the checkbox is unchecked, but by setting it to true, it should have the opposite effect.

Fierce answered 15/5, 2011 at 22:15 Comment(2)
Tested it myself and this does exactly what OP requested, all within the XML =].Capreolate
The attribute has to be added to the preference which others depend on, not on the dependent preference. Imho it would have been better if the choice had been done by the dependent preference... what if two dependent preferences need to behave different according to the value of another preference? anyway...Commendatory
P
9

I don't think there's any out-of-the-box solution for it, i.e. an inverted dependancy attribute. But there's always the click-listener:

preference1.setOnPreferenceClickListener(pref1_click);

....

private OnPreferenceClickListener pref1_click = new OnPreferenceClickListener() {
    public boolean onPreferenceClick(Preference preference) {
        preference2.setEnabled(!preference1.isChecked());
        return true;
    }
}
Pentangular answered 20/4, 2010 at 6:15 Comment(0)
P
0

Android CheckBox??


I am assuming you are using the Android.widget.checkBox:

http://developer.android.com/reference/android/widget/CheckBox.html

Try this

 public class MyActivity extends Activity {
     protected void onCreate(Bundle icicle) {
         super.onCreate(icicle);

         setContentView(R.layout.content_layout_id);

         final CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkbox_id);
         if (checkBox1.isChecked()) {
             checkBox2.setChecked(false);
         }
     }
 }

GoodLUCK!!

Plexiform answered 20/4, 2010 at 6:17 Comment(1)
No I am using android.preference.CheckBoxPreference NOT Android.widget.checkBox. AnyWay Thanks for your concern.Raymonraymond

© 2022 - 2024 — McMap. All rights reserved.