Reputation: 1402
I want to create a preference screen in which there are three checkboxes; the first one is clickable and the other two are not until the first is checked.
How can I do this? I've seen this tutorial, but there is only one checkbox. Can anyone help me?
Upvotes: 4
Views: 7815
Reputation: 1346
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
android:summary="@string/summary_category"
android:title="@string/title_category">
<CheckBoxPreference
android:key="main"
android:defaultValue="true"
android:summary="@string/summary_main"
android:title="@string/title_main"
/>
<CheckBoxPreference
android:key="firstDependent"
android:summary="@string/summary_firstDependent"
android:title="@string/title_firstDependent"
android:dependancy="main"
/>
<CheckBoxPreference
android:key="secondDependent"
android:summary="@string/summary_secondDependent"
android:title="@string/title_secondDependent"
android:dependancy="main"
/>
</PreferenceCategory>
<!--Any other categories include here-->
</PreferenceScreen>
You can do this simply by setting android:dependancy
to the key of the check box in which the respective check boxes must depend on.
Now create a folder named xml in res folder and put your preferences xml file inside that. Then do the following.
public class SettingsActivity extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
You can also do this with fragments which is more recommended. But the above method is much more easy. If you want to do that with fragments, check this which contains everything you need to know about creating a Settings Activity.
Hope this helps.
Upvotes: 5
Reputation: 8145
You have to do it like in that example, but you'll have three checkboxes
instead of one. If you want two checkboxes
to be disabled until the first one is true you can use the android:dependency
property. With this property you need to specify the key of the preference they will depend on.
<PreferenceCategory
android:summary="..."
android:title="..." >
<CheckBoxPreference
android:defaultValue="true"
android:key="first"
android:summary="@string/summary_first"
android:title="@string/title_first" />
<CheckBoxPreference
android:defaultValue="false"
android:dependency="first"
android:key="second"
android:summary="@string/summary_second"
android:title="@string/title_second" />
<CheckBoxPreference
android:defaultValue="false"
android:dependency="first"
android:key="third"
android:summary="@string/summary_third"
android:title="@string/title_third" />
</PreferenceCategory>
Upvotes: 3