PreferenceFragmentCompat requires preferenceTheme to be set

AndroidAndroid FragmentsAndroid Support-LibraryPreferencefragmentPreference V7

Android Problem Overview


With the new PreferenceFragmentCompat from the v7 Preference Support Library: http://developer.android.com/tools/support-library/features.html#v7-preference, I get this error

E  java.lang.IllegalStateException: Must specify preferenceTheme in theme
E      at android.support.v7.preference.PreferenceFragmentCompat.onCreate(PreferenceFragmentCompat.java:202)

What theme should be set?

Update: I have tried using

<item name="preferenceTheme">@style/PreferenceThemeOverlay</item>

as suggested by @Bogato but it doesn't look right and looks very Holo even on Lollipop.

Support library:

enter image description here

Native preferences:

enter image description here

Android Solutions


Solution 1 - Android

> The sample project can be found here

The bugfix is available as a gradle dependency

Download

Now one can use the library pretty easy. Here are quickest way to do so, but you should check out the README for more info.

1. Update your module's gradle file:

compile 'com.takisoft.fix:preference-v7:27.0.0.0'

2. Use the appropriate class as your fragment's base

You can use either PreferenceFragmentCompat or PreferenceFragmentCompatDividers.

(Watch out for the appropriate package name when importing PreferenceFragmentCompat!)

3. Use the appropriate theme

Set your containing Activity's theme to a variant of @style/PreferenceFixTheme, like NoActionBar, Light, etc.

> For more info and usage tips, go to the project's page.


P.S. In this section you could find the detailed solution that led to creation of the library, but I decided to remove it because it might be confusing. If you're curious about the steps, you can still find them in the revision history of this answer.

Solution 2 - Android

You have to specify preferenceTheme in your preference activity's theme.

For example :

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

PreferenceThemeOverlay is the default theme which comes with preference-v7 support library.

Solution 3 - Android

Seems like Google fixed this issue. I've tested this with the preference v14-support version 25.3.1

  1. Add implementation 'com.android.support:preference-v14:25.3.1' to your Gradle.

  2. Add PreferenceThemeOverlay.v14.Material to the style instead of PreferenceThemeOverlay.

  3. Finally, add the style to the Manifest

    android:theme="@style/AppTheme.SettingsTheme"

Solution 4 - Android

To use the PreferenceFragmentCompat 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.

Solution 5 - Android

I just added this line in theme and it working perfect on API 19 and above.

<item name="preferenceTheme">@style/PreferenceThemeOverlay.v14.Material</item>

Solution 6 - Android

Add this code to your styles.xml and modify it according. It should work fine.

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
</style>

<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>

Solution 7 - Android

Solution that worked for me in API 25. I had this default theme:

<style name="AppTheme.NoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

And added this line to it

<style name="AppTheme.NoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
</style>

PreferenceThemeOverlay was was already defined, I didn't have to include it. It worked on Android 4.x and 5.x devices of my own

Solution 8 - Android

I added the "preferenceTheme" item into my style and that seemed to fix the issue:

   <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
     <item name="android:actionBarStyle">@style/MyActionBarTheme</item>
     <item name="colorPrimary">@color/ColorPrimary</item>
     <item name="colorPrimaryDark">@color/ColorPrimaryDark</item>
     <item name="preferenceTheme">@style/Preference</item>
 </style>

Solution 9 - Android

The most helpful site for me: Medium

Author shows that we need to add two packages: com.android.support:preference-v7 and com.android.support:preference-v14, because the first one it being distributed without styles.

Let me know if it works

Solution 10 - Android

The accepted "Takisoft Fix" seems rather kludgy and after playing around with it, I am still not satisfied with the resulting look.

If you need API 9+ PreferenceFragment support library, I recommend using Material Preference Support Library. It works perfectly (at least for me).

Solution 11 - Android

The problem occurs because the AAPT(Android Asset Packaging Tool) cannot find some resources. Fix this issue as follows:

1.Copy resources which are necessary to a new responding resource folder, named like res_fw_v7_preference or something else.

2.Using gradle to build your application and add some additional AAPT options in build.gradle(module's) like this:

android {
    ...
    aaptOptions {
        additionalParameters '-S',
            'src/main/res_fw_v17_leanback',  // resources in frameworks/support/v17/leanback
            '-S',
            'src/main/res_fw_v7_preference',  // resources in frameworks/support/v7/preference
            '-S',
            'src/main/res_fw_v14_preference',  // resources in frameworks/support/v14/preference
            '-S',
            'src/main/res_fw_v17_preference-leanback',  // resources in frameworks/support/v17/preference-leanback
            '-S',
            'src/main/res_fw_v7_appcompat',  // resources in frameworks/support/v7/appcompat
            '-S',
            'src/main/res_fw_v7_recyclerview',  // resources in frameworks/support/v7/recyclerview
            '-S',
            'src/main/res',  // resources in your application
            '--auto-add-overlay',
            '--extra-packages',
            'android.support.v17.leanback:android.support.v7.preference:android.support.v14.preference:android.support.v17.preference:android.support.v7.appcompat:android.support.v7.recyclerview'
    noCompress 'foo', 'bar'
    ignoreAssetsPattern '!.svn:!.git:!.ds_store:!*.scc:.*:<dir>_*:!CVS:!thumbs.db:!picasa.ini:!*~'
    }
    ...
}

Hope this is helpful:)

Solution 12 - Android

Thanks @sergio for answer, using <item name="preferenceTheme">@style/PreferenceThemeOverlay.v14.Material</item> instead of <item name="preferenceTheme">@style/Preference</item> works great for me.

<style name="IntentTheme" parent="Theme.AppCompat">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:actionModeBackground">@color/fulltransparent</item>
        <item name="preferenceTheme">@style/PreferenceThemeOverlay.v14.Material</item>

Solution 13 - Android

Just let it go and use API 11+.

API 7 is almost seven years old now.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIntricationsView Question on Stackoverflow
Solution 1 - AndroidGergely KőrössyView Answer on Stackoverflow
Solution 2 - AndroidBogatoView Answer on Stackoverflow
Solution 3 - AndroidSrikar ReddyView Answer on Stackoverflow
Solution 4 - AndroidGabriele MariottiView Answer on Stackoverflow
Solution 5 - AndroidPanacheView Answer on Stackoverflow
Solution 6 - AndroidPrashant PaliwalView Answer on Stackoverflow
Solution 7 - AndroidvoghDevView Answer on Stackoverflow
Solution 8 - AndroidSergio GeralnikView Answer on Stackoverflow
Solution 9 - Androidmacieg_bView Answer on Stackoverflow
Solution 10 - AndroidshredEngineerView Answer on Stackoverflow
Solution 11 - AndroidJasonView Answer on Stackoverflow
Solution 12 - AndroidQueendevelopersView Answer on Stackoverflow
Solution 13 - AndroidtransView Answer on Stackoverflow