Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 893
Vasu Avatar asked Apr 20 '10 06:04

Vasu


People also ask

Where is the preference option in Android Studio?

The preference option doesn't exist anymore. You will need to right click the res -> new -> Android resource file and choose the resource type as xml in the dropdown. Then you will manually need to add the layout for preference xml.

What is preference activity Android?

PreferencesActivity is a way to easily create preference screens such as those in Android itself, just look under Settings . These can be used inside applications to easily save preferences to SharedPreferences and then easily access these from within your app. See this page for more information on PreferenceActivity.

How do I turn off preferences?

Programmatically: getPreferenceScreen(). findPreference("yourpref"). setEnabled(false);


1 Answers

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.

like image 125
clauziere Avatar answered Sep 20 '22 15:09

clauziere