I have a written a custom TextView in kotlin, which acts as a selection button. Meaning, in unselected states it has a grey-ish background with corners, and in selected state it this background changes color.
(In the middle of the button there's a text)
The problem : I need to display a whole group of these buttons, and for each button, the color of selected state can vary and change from button to button.
So if I have : Button1, Button2, Button3, for each of them, when they're selected, the background color is different.
Can I somehow control the color that the drawable xml uses?
button_background :
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle" >
<solid android:color="@color/dynamicColor" /> --> how can I control this color?
<corners
android:bottomLeftRadius="20dp"
android:bottomRightRadius="20dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp" />
</shape>
Yes, you can change the Solid color
of the drawable file programmatically. like the following.
val drawable = view.getBackground() as GradientDrawable // here view is your textview reference.
drawable.setColor(Color.GREEN) // set your color here
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments