How to use the v7/v14 Preference Support library?
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>
hidro's answer is right but one more thing here to notice:
Just use normal preference xml tags such as PreferenceScreen
instead of the full class name. The support library will convert them automatically.
Why: If you use the full class name the code suggestion and layout preview will not work properly.
So you should write xml like this:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
...>
<ListPreference
... />
<SwitchPreferenceCompat
... />
...
</PreferenceCategory>
...
</PreferenceScreen>
With the new preference support library v7 you can use the PreferenceFragmentCompat with any Activity
or AppCompatActivity
public static class PrefsFragment extends PreferenceFragmentCompat {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
}
You have to set preferenceTheme
in your theme:
<style name="AppTheme" parent="@style/Theme.AppCompat.Light">
...
<item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
</style>
In this way you can customize the preferenceTheme
to style the layouts used for each preference type without affecting other parts of your Activity.