views:

351

answers:

3

Hi,

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?

+3  A: 

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.getEnabled());
    }
}
David Hedlund
A: 

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!!

CVS-2600Hertz
Hey, Check out DAVID's solution too...
CVS-2600Hertz
No I am using android.preference.CheckBoxPreference NOT Android.widget.checkBox. AnyWay Thanks for your concern.
Kaillash