custom TextView that can change background color

BVtp

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>
Jakir Hossain

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.

edited at
0

Comments

0 comments
Login to comment

Related

Can not change background color

Custom ListCellRenderer will not change background color

How to change TextView's background color dynamically?

How to change the background color of a TextView inside of an ActionMode

Change text Background color of Specific word in a TextView

How to change the resource background color of TextView?

how to change color of textView of custom griditem in android

Can't change background color

Android: How can I change AlertDialog Title Text Color and Background Color without using custom layout?

how to change textView color when scrolled on same color background?

How can I change letters' color in a custom TextView control well in android?

Failed to change custom actionbar background color in android

cannot change background color of custom editText in android

How can I change the background color of the Android tab with a renderer, while using a custom renderer to add some padding

How can I change background color and add custom font into my introduction scene in flutter?

Programatically make TextView background color change while pressed

How to change TextView background color while it's pressed down?

Textview value must change itemView background color with if statement

Set Background Color for a textview

Custom keyboard shortcuts for Google Docs (change color - background color)

Change custom text field background color and text color IOS Swift

Status bar background color does not change to custom color

Change background color for a specific box to a custom color in Shinydashboard

Fullcalendar - can't change background color of calendar

How can I change background color of PageViewController?

How can I change background color for the desktop?

Can't change the background color through java

Can I change the background color for italic text?

Can't change background color to ContentView