Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
892 views
in Technique[技术] by (71.8m points)

androidx - Android icons and radio buttons not following day/night mode

After creating a very basic Settings AppCompatActivity and PreferenceFragmentCompat to change the day/night mode of my application, but the light theme doesn't seem to apply across the entire application. None of the icons in the rest of the application want to follow, and the radio buttons in the ListPreference dialog also seem to retain their original dark theme. What am I doing wrong here?

System/Dark

Light

SettingsActivity.java

public class SettingsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        getSupportFragmentManager().beginTransaction().replace(R.id.settingsFrame, new SettingsFragment()).commit();
    }

    public static class SettingsFragment extends PreferenceFragmentCompat {

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

            findPreference(getString(R.string.pref_theme)).setOnPreferenceChangeListener((preference, newValue) -> {
                if (newValue.toString().equals(getString(R.string.light))) {
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                } else if (newValue.toString().equals(getString(R.string.dark))) {
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                } else {
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
                }
                return true;
            });
        }
    }
}

settings.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <ListPreference
        android:key="@string/pref_theme"
        android:icon="@drawable/ic_day_24"
        android:title="@string/theme"
        app:useSimpleSummaryProvider="true"
        android:entries="@array/themes"
        android:entryValues="@array/themes"
        android:defaultValue="@string/system"/>
</PreferenceScreen>

ic_day_24.xml

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24"
    android:viewportHeight="24"
    android:tint="?attr/colorControlNormal">
  <path
        android:fillColor="@android:color/white"
        android:pathData="M20,8.69L20,4h-4.69L12,0.69 8.69,4L4,4v4.69L0.69,12 4,15.31L4,20h4.69L12,23.31 15.31,20L20,20v-4.69L23.31,12 20,8.69zM12,18c-3.31,0 -6,-2.69 -6,-6s2.69,-6 6,-6 6,2.69 6,6 -2.69,6 -6,6zM12,8c-2.21,0 -4,1.79 -4,4s1.79,4 4,4 4,-1.79 4,-4 -1.79,-4 -4,-4z"/>
</vector>

themes.xml

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.FloatingTimer" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <item name="colorPrimary">@color/red</item>
        <item name="colorPrimaryVariant">@color/blue_dark</item>
        <item name="colorOnPrimary">@color/white</item>
        <item name="colorSecondary">@color/orange_normal</item>
        <item name="colorSecondaryVariant">@color/orange_dark</item>
        <item name="colorOnSecondary">@color/white</item>
        <item name="android:statusBarColor">?attr/colorPrimaryVariant</item>
        <item name="colorSurface">@color/blue_normal</item>
        <item name="colorOnSurface">@color/white</item>
        <item name="colorControlNormal">@color/white</item>
    </style>

    <style name="Theme.FloatingTimer.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
</resources>

theme.xml (night)

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.FloatingTimer" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <item name="colorPrimary">@color/red</item>
        <item name="colorPrimaryVariant">@color/blue_dark</item>
        <item name="colorOnPrimary">@color/white</item>
        <item name="colorSecondary">@color/orange_normal</item>
        <item name="colorSecondaryVariant">@color/orange_dark</item>
        <item name="colorOnSecondary">@color/white</item>
        <item name="android:statusBarColor">?attr/colorPrimaryVariant</item>
        <item name="colorSurface">@color/blue_normal</item>
        <item name="colorOnSurface">@color/white</item>
        <item name="colorControlNormal">@color/white</item>
    </style>

    <style name="Theme.FloatingTimer.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
</resources>

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="blue_light">#FF6E9FCF</color>
    <color name="blue_normal">#FF3F7FBF</color>
    <color name="blue_dark">#FF2F6090</color>
    <color name="orange_light">#FFD97F3F</color>
    <color name="orange_normal">#FFCC5500</color>
    <color name="orange_dark">#FF9A4000</color>
    <color name="black">#FF000000</color>
    <color name="white">#FFFFFFFF</color>
    <color name="translucent_black">#CC000000</color>
    <color name="translucent_dark_gray">#CC333333</color>
    <color name="translucent_light_gray">#CCCCCCCC</color>
    <color name="translucent_white">#CCFFFFFF</color>
    <color name="translucent_overlay">@color/translucent_white</color>
    <color name="list_icon">@color/black</color>
</resources>

colors.xml (night)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="translucent_overlay">@color/translucent_dark_gray</color>3
    <color name="list_icon">@color/white</color>
</resources>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Well I played myself by using <item name="colorControlNormal">@color/white</item> in my themes. After removing that the icon colors are correct now.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...