I\'ve come to a sudden halt in the development of my app as I realized that PreferenceFragments weren\'t supported in this library. Are there any alternatives that a rookie
Preferences Support Library: Preference Fragments for API 7+, no matter the Activity
A simple implementation would include a PreferenceFragmentCompat such as:
public class PreferencesFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
addPreferencesFromResource(R.xml.preferences);
}
}
You’ll also need to set preferenceTheme in your theme:
<style name="AppTheme" parent="@style/Theme.AppCompat.Light">
<item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
</style>
And add this in dependencies: http://developer.android.com/tools/support-library/features.html
com.android.support:preference-v14:23.1.0
You can use my own PreferenceFragment.
It's simple and I had no issues with it so far. I think you can only display one of them at a time in a single Activity, which should be OK.
You can extend from PreferenceActivity instead, and if you wish to have an ActionBar, you can add it using a Toolbar as being above the ListView used for the preferences.
This can be done by using a vertical LinearLayout which holds the Toolbar and a ListView with android:id="@android:id/list" .
If you wish, you can see my solution here .
Important Update: The latest revision of the v7 support library
now has a native PreferenceFragmentCompat.
I am using this library, which has an AAR
in mavenCentral
so you can easily include it if you are using Gradle
.
compile 'com.github.machinarius:preferencefragment:0.1.1'
Support-v7 library now includes PreferenceFragmentCompat
. So it will be a better idea to use it.
Add the following project as a library project to your application.
https://github.com/kolavar/android-support-v4-preferencefragment
You can keep everything including your fragment transaction as it is. When importing the PreferenceFragment
class, make sure the correct import header is user.
import android.support.v4.preference.PreferenceFragment;
instead of
import android.preference.PreferenceFragment;
I know this is an old question, but you can now use PreferenceFragmentCompat from com.android.support:preference-v7:23.3.0