Ric868
Ric868

Reputation: 89

Android when use PreferenceCategory and when PreferenceScreen

I am learning about android but i do not understand the difference between PreferenceCategory and PreferenceScreen, i saw that many tutorial used the second one instead the first one, but i do not understand why. When you recommend me to use the first one instead the second or vice versa. Another thing, does these two codes made the same thing?

<PreferenceCategory
    android:title="first">
    <CheckBoxPreference
        android:key="first_preferences"
        android:title="first"
        android:defaultValue="false" />`
</PreferenceCategory>

and the second code:

<PreferenceScreen
    android:title="second">
    <CheckBoxPreference
        android:key="second_preferences"
        android:title="second"
        android:defaultValue="false" />`
</PreferenceScreen>

thanks for the help.

Upvotes: 0

Views: 4868

Answers (2)

Bhuvanesh BS
Bhuvanesh BS

Reputation: 13617

PreferenceScreen is a container of preferences.

Inside the PreferenceScreen, you can categorize the content by PreferenceCategory. Below example will show you the difference between PreferenceScreen and PreferenceCategory.

Example:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <PreferenceCategory android:title="USER PROFILE" >
        <EditTextPreference 
                android:title="Set username" 
                android:summary="Set your username" 
                android:key="prefUsername"/>
    </PreferenceCategory>

    <PreferenceCategory android:title="UPDATE SETTINGS" >
        <CheckBoxPreference
            android:defaultValue="false"
            android:key="prefSendReport"
            android:summary="Helps to fix bugs"
            android:title="Send crash reports" >
        </CheckBoxPreference>

        <ListPreference
            android:key="prefSyncFrequency"
            android:entries="@array/syncFrequency"
            android:summary="@string/pref_sync_frequency_summary" 
            android:entryValues="Helps to fix bugs"
            android:title="Sync frequency" />
    </PreferenceCategory>

</PreferenceScreen>

The above XML will produce the below output.

enter image description here

Now you can see the PrefereceCategory is categorizing the content.

Upvotes: 2

Phant&#244;maxx
Phant&#244;maxx

Reputation: 38098

A preference Screen is the root layout which contains the settings.
A preference Category is a "title" for a group of related settings.

Upvotes: 2

Related Questions