Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Dark Theme does not affect SVG images

My current Android application is employing the following style in an attempt to enable Dark Theme

<resources>
    <style name="AppTheme" parent="@style/Theme.MaterialComponents.DayNight.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorError">@color/design_default_color_error</item>
        <item name="android:textColor">?attr/colorOnBackground</item>
    </style>

    <style name="splashDialog" parent="Theme.AppCompat.Dialog">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowActionBar">false</item>
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowBackground">@null</item>
    </style>
</resources>

Everything in the UI works fine apart from this SVG image which seems to be the same in both light and dark themes

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:tint="?attr/colorControlNormal"
    android:viewportWidth="24.0"
    android:viewportHeight="24.0">
    <path
        android:fillColor="@android:color/white"
        android:pathData="M12,22c1.1,0 2,-0.9 2,-2h-4c0,1.1 0.9,2 2,2zM18,16v-5c0,-3.07 -1.63,-5.64 -4.5,-6.32L13.5,4c0,-0.83 -0.67,-1.5 -1.5,-1.5s-1.5,0.67 -1.5,1.5v0.68C7.64,5.36 6,7.92 6,11v5l-2,2v1h16v-1l-2,-2zM16,17L8,17v-6c0,-2.48 1.51,-4.5 4,-4.5s4,2.02 4,4.5v6z" />
</vector>

How can I achieve the desired result of a "lighter" image when in Dark mode?

like image 742
Hector Avatar asked Dec 05 '25 05:12

Hector


1 Answers

In my project I used negative filter for drawable, like this:

private val negative = floatArrayOf(
    -1.0f,     .0f,     .0f,    .0f, 255.0f, // red
    .0f,     -1.0f,     .0f,    .0f, 255.0f, // green
    .0f,       .0f,   -1.0f,    .0f, 255.0f, // blue
    .0f,       .0f,     .0f,   1.0f,   .0f  // alpha
)

fun Drawable.toNegative() {
    this.colorFilter = ColorMatrixColorFilter(negative)
}
like image 169
Artem Botnev Avatar answered Dec 07 '25 20:12

Artem Botnev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!