banner
RustyNail

RustyNail

coder. 【blog】https://rustynail.me 【nostr】wss://ts.relays.world/ wss://relays.world/nostr

TextView dynamically changes the background color of Drawble.

When encountering the need to dynamically change the color of a Drawable, record the following:

Create a shape in /drawable/t_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/darkBG"></solid>
    <corners android:radius="4dp"/>
</shape>

Then, the color needs to be dynamically changed.

First, obtain the Drawable:

Drawable tagShapeDrawable = Drawable.createFromXml(context.getResources(), context.getResources().getXml(R.drawable.tag_shape));

Then:

tagShapeDrawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
view.setBackground(tagShapeDrawable);
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.