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?
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?
You are right it doesn't exist on the appcompat v7, but Google actually added AppCompatDelegate
abstract class as a delegate you can use to inject the AppCompat's support to any activity. You can find more from this answer.
This is an example of how to inject the AppCompatDelegate
into your activity from the AppCompat's samples from Google, You can find it here.
I tried to implement Hidro's answer above with an activity which also contained a toolbar and it gave me the following a layout inflation exception because of the following error:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.support.v4.app.FragmentHostCallback.getContext()' on a null object reference
I haven't been able to solve these, so have resorted to the following:
public class SettingsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState == null)
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, new SettingsFragment()).commit();
}
}
With the following layout for the SettingsActivity:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<include layout="@layout/toolbar"/>
<FrameLayout android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
Posted here as it may help if other people encounter the same exception
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.
You have to extend
AppCompatActivity
, which is required for fragment, and include a subclass ofPreferenceFragmentCompat
. 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 apreferenceTheme
attribute.Read the announcement here. With preference-v7 library you can replace
PreferenceFragment
(API 11+) withPreferenceFragmentCompat
subclass, andSwitchPreference
(API 14+) withSwitchPreferenceCompat
and have your settings screen work from API 7.Below is how I made it work:
SettingsActivity.java
layout/activity_settings.xml
SettingsFragment.java
xml/preferences.xml
values/styles.xml
preference-v7 default theme