Taki
Taki

Reputation: 3730

Changing Switch Preference Color

I have settings xml file where i have checkboxpreferences and a switchpreference , when i switch to dark mode , the tint of the switch and checkbox don't automatically change according to the selected mode here is an image of what i'm talking

enter image description here

So the switchpreference is supposed to be white when app is in dark mode but it is not changing , can anyone help me to fix it , thank you

*This is my setting xml file

<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <Preference
        android:title="@string/YourPreferredLanguage"
        android:icon="@drawable/ic_baseline_language_24"/>

        <CheckBoxPreference
            android:title="@string/French"
            android:key="french"
            />
        <CheckBoxPreference
            android:title="@string/English"
            android:key="english"
            />
        <CheckBoxPreference
            android:title="@string/spanish"
            android:key="spanish"
            />
        <CheckBoxPreference
            android:title="@string/Portuguese"
            android:key="portuguese"
            />


    <Preference
        android:key="preferences"
        android:title="@string/rateourapp"
        android:icon="@drawable/ic_baseline_stars_24"/>
        <taki.food.foodappv.RatingPreferences
            android:defaultValue=""
            android:key="customlayout"/>

    <Preference
        android:title="@string/switchmodes"
        app:allowDividerAbove="true"
        android:icon="@drawable/ic_baseline_perm_data_setting_24"/>
        <SwitchPreference
            android:key="switchmode"
            android:title="Light Mode ON"/>

</androidx.preference.PreferenceScreen>

Upvotes: 0

Views: 1747

Answers (1)

Abhinav Chauhan
Abhinav Chauhan

Reputation: 1394

the colorAccent in colors.xml/styles.xml defines the color of checkboxes/rating bar ,progressbar/switch and for many other sdk widgets like some color is datePicker. colorPrimary sets the color for Action Bar colorPrimaryDark sets the color for status bar they show up in many other places too i don't remember right now but you will notice with time.

also evidently the text color is not set by colorPrimary because by default the text is gray and your colorPrimary is not gray, the text color comes from an app theme attribute called textColorwhich is used by the TextView internally you can override it in the app theme open the styles.xml in the style named AppTheme include <item name="android:textColor">your color</item> to change the text color in the whole app

there are many other things you can override from the parent theme by default android studio just overrides colorAccent colorPrimary and colorPrimaryDarkyou can for example override windowBackground incude <item name="android:windowBackground">@color/windowBackground</item> in AppTheme to change the background which is by default white for all apps there is much more stuff to customize for example overriding the default elevation for dialogs, or default text size or font etc.

so change the default color for switch and ratingbar you need to override colorAccent if that color comes from the color.xml of values-night and is also in values folder's colors.xml the android chooses the correct one according the the current mode.

Have a good day

Love from india

Upvotes: 2

Related Questions