Search code examples
androidandroid-support-librarysupport-preference

How to use the v7/v14 Preference Support library?


Together with the M release, there are new support libraries. One of them that seems to be very useful is the v7 Preference Support library.

It does not seem to have PreferenceActivity or something similar, how do we integrate it to our app?


Solution

  • You have to extend AppCompatActivity, which is required for fragment, and include a subclass of PreferenceFragmentCompat. The abstract fragment requires to override one method, in which you should place your preference inflation logic. And last, your activity theme needs to specify a preferenceTheme attribute.

    Read the announcement here. With preference-v7 library you can replace PreferenceFragment (API 11+) with PreferenceFragmentCompat subclass, and SwitchPreference (API 14+) with SwitchPreferenceCompat and have your settings screen work from API 7.

    Below is how I made it work:

    SettingsActivity.java

    public class SettingsActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_settings);
        }
    }
    

    layout/activity_settings.xml

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent" android:layout_height="match_parent" >
        <fragment
            android:name=".SettingsFragment"
            android:tag=".SettingsFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </FrameLayout>
    

    SettingsFragment.java

    public class SettingsFragment extends PreferenceFragmentCompat {
    
        @Override
        public void onCreatePreferences(Bundle bundle, String s) {
            addPreferencesFromResource(R.xml.preferences);
        }
    }
    

    xml/preferences.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent" android:layout_height="match_parent">
    
        <android.support.v7.preference.PreferenceCategory
            ...>
    
            <android.support.v7.preference.ListPreference
                ... />
    
            <android.support.v7.preference.SwitchPreferenceCompat
                ... />
    
            ...
    
        </android.support.v7.preference.PreferenceCategory>
    
        ...
    
    </android.support.v7.preference.PreferenceScreen>
    

    values/styles.xml

    <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
        ...
    </style>
    

    preference-v7 default theme

    <style name="PreferenceThemeOverlay">
        <item name="preferenceScreenStyle">@style/Preference.PreferenceScreen</item>
        <item name="preferenceFragmentStyle">@style/PreferenceFragment</item>
        <item name="preferenceCategoryStyle">@style/Preference.Category</item>
        <item name="preferenceStyle">@style/Preference</item>
        <item name="preferenceInformationStyle">@style/Preference.Information</item>
        <item name="checkBoxPreferenceStyle">@style/Preference.CheckBoxPreference</item>
        <item name="switchPreferenceCompatStyle">@style/Preference.SwitchPreferenceCompat</item>
        <item name="dialogPreferenceStyle">@style/Preference.DialogPreference</item>
        <item name="editTextPreferenceStyle">@style/Preference.DialogPreference.EditTextPreference</item>
        <item name="preferenceFragmentListStyle">@style/PreferenceFragmentList</item>
    </style>