1

I know this might be a possible duplicate like

Applying AppCompat theme to individual preferences in a PreferenceFragment

I used this video on youtube: https://www.youtube.com/watch?v=PS9jhuHECEQ

And from devolper android this guideline: https://developer.android.com/guide/topics/ui/settings

to build my preferences. At the moment my preference library is:

implementation 'androidx.preference:preference:1.1.0-beta01'

So now I want to apply my apptheme to my preferences settings...

I got this in my manifest.xml

    <application
        android:name=".XXXApplication"
        android:allowBackup="false"
        android:label="@string/W_APP_NAME"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:ignore="GoogleAppIndexingWarning">

        <activity
            android:name=".Views.MainActivity"
            android:label="@string/W_APP_NAME"
            android:screenOrientation="landscape"
            android:theme="@style/AppTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

My styles.xml

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- 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>

        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

Now I have a root_preferences.xml which holds several other preferences, like this:

root_preferences.xml

<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <Preference
            app:title="@string/N_CAM_ITEM"
            app:summary="@string/P_CAMERA_SETTINGS"
            app:icon="@drawable/ic_menu_camera"
            app:fragment="com.example.XXX.Views.CameraSettingFragment"/>

And there is also a camera_preferences.xml which is also a PreferenceScreen that holds SwitchPreferences and so on...

If I navigate to settings my SettingsFragment is called..

    public class SettingsFragment extends PreferenceFragmentCompat {

    @Override
    public void onCreatePreferences(@Nullable Bundle savedInstanceState, @Nullable String rootKey) {
        setPreferencesFromResource(R.xml.root_preferences, rootKey);

    }

So what am I missing, that my theme/style is not applied to those preferences? Hope you can help me out with this!

Update How my settings look enter image description here

So checkbox and switch aren't showing any colors... There must be some primary orange green color

0stack
  • 51
  • 8
  • 1
    Preference API is pretty broken, I highly recommend checking out [Preference Fix Library](https://github.com/Gericop/Android-Support-Preference-V7-Fix) which handles a lot of the issues including missing material theme. – Pawel Jun 27 '19 at 12:21
  • 1
    Could be a case that it is broken, but if I take a look at this official preference sample: https://github.com/googlesamples/android-preferences the apptheme style is however applied – 0stack Jun 27 '19 at 12:49
  • What do you mean by 'my theme/style is not applied to those preferences?' - could you attach a screenshot? Also, there shouldn't be any issues regarding 'missing material theme' - a lot of bugs have been fixed since the fix library you linked was created. If you noticed something in particular, you can file a bug [here](https://issuetracker.google.com/issues/new?component=460913&template=1176948) – Louis Jun 27 '19 at 13:51
  • I have updated my question with a screenshot - this behaviour was in earlier versions too.. – 0stack Jun 27 '19 at 14:56
  • Have you attempted setting the style on the individual preference object in xml? I realize this may not be the ideal solution, and there should be one that works with how you are doing it, but curious if you have tried to debug in this way yet. – Elli White Jun 27 '19 at 15:16
  • I have tried to attempt the style over including `android:theme="@style/AppTheme"` in every `PreferenceScreen`in xml but there isn't any chance - am I the only one who is facing this issue? – 0stack Jun 27 '19 at 15:20
  • 1
    Are you using an AppCompatActivity as your base activity? What's your colorAccent set to? – Louis Jun 27 '19 at 16:44
  • Oh man really - I changed primaryDark and ColorAccent just to verify and indeed there is now my primary color in the settings.. :) But why? I thought that primary color should be the color of choice for titles and switches and so on?!?! And it is AppCompatActivity! Thanks @Louis – 0stack Jun 28 '19 at 07:24
  • :) Happy to help. Yes, colorAccent is always used for these types of components - primary color is normally used for things like the action bar. – Louis Jun 28 '19 at 11:28

1 Answers1

0

As @Louis pointed out in the comments ColorAccent is the way to go to achieve colored elements in the preference settings. Many thanks Louis!

0stack
  • 51
  • 8