how to change GradientDrawable color on android 13

38 views Asked by At

I want to change the color of the GradientDrawable. Here is how I do it. This works on android 12 and earlier but can't apply on android 13.

I have selected_dot.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/item_selected_dot">
        <shape
            android:innerRadius="0dp"
            android:shape="ring"
            android:thickness="@dimen/margin_5dp"
            android:useLevel="false">
            <gradient
                android:startColor="@color/colorStart"
                android:endColor="@color/colorEnd"/>
        </shape>
    </item>
</layer-list>

In activity, I change the color in the following way:

val layerDrawable =(ContextCompat.getDrawable(this,R.drawable.selected_dot) as LayerDrawable)
      .findDrawableByLayerId(R.id.item_selected_dot) as GradientDrawable
layerDrawable.colors = intArrayOf(0XFF3879E9.toInt(), 0XFF7F6DED.toInt())

Please give to me solution.thanks!!

0

There are 0 answers