How to programmatically change the value of the android: tint property in <bitmap xml

108 Views Asked by At

I'm new here and I need your help. Please tell me how to access the layer-list xml structure from the program level and dynamically change the "tint" color of the bitmap from the program level.

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/bg_peper" android:right="65dp">
        <bitmap
            android:gravity="top|left"
            android:tint="@color/red"
            android:src="@drawable/ic_favorite" />
    </item>
    <item....   
</layer-list>
1

There are 1 best solutions below

0
Navjot On

Change the tint dynamically using this

ImageView view = (ImageView)findViewById(R.id.tintLayerView); // ImageView where you have added the drawable as src
LayerDrawable layerDrawable = (LayerDrawable)view.getDrawable();
Drawable bpPeperDrawable = layerDrawable.findDrawableByLayerId(R.id.bg_peper);
DrawableCompat.setTint(bpPeperDrawable.mutate(), ContextCompat.getColor(this, R.color.red)); // Use the required color here