I have CheckBoxPreference and 2 others: one is Edit Test Pref. and another is ListBox Pref. How I can enable list box pref and disable edit text pref. when CheckBoxPreference is turned on?
Just to simplify the answer from Sergio,
android:dependency="keyOfParent"
This makes the item dependent on the parent item, may need to be a switch.
Also adding a listener to the onSharedPreferenceChanged works, sometimes.. it sometimes doesn't work as desired ( not sure why )
Add
public class YourClass extends Whatever implements SharedPreferences.OnSharedPreferenceChangeListener
Then after OnCreate()
@Override
public void onSharedPreferenceChanged (SharedPreferences p1, String p2)
{
if (Your Arguments)
{
// getPreferenceScreen().findPreference("pref_key").setEnabled(false);
}
}
As a variant, it's possible to put the "dependency" into the ListBoxPref.
<PreferenceCategory
android:key="key1"
android:title="@string/title1">
<SwitchPreference
android:key="parents"
android:summaryOff="@string/do_smthng"
android:summaryOn="@string/do_smthng"
android:switchTextOff="@string/i_am_sleeping"
android:switchTextOn="@string/i_have_free_time" />
<CheckBoxPreference
android:key="baby"
android:dependency="parents"
android:title="@string/basic_habbits"
android:summaryOff="@string/play_with_parents"
android:summaryOn="@string/play_with_parents"
android:defaultValue="true" />
</PreferenceCategory>
Basically, baby can't play with parents when they are sleeping =)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With