TootsieRockNRoll
TootsieRockNRoll

Reputation: 3288

Android - Custom view text color Selector

I created a custom view which contains a TextView inside and at some point I will need to change the text colour.

Using a standard declare-styleable with <attr name="android:textColor"/> works.

  <declare-styleable name="CustomStatusButton">
    <attr name="android:text"/>
    <attr name="android:textColor"/>
    <attr name="android:enabled"/>
  </declare-styleable>

I use this to set the colour:

  val textColor = typedArray.getColor(R.styleable. CustomStatusButton_android_textColor, Color.BLACK)
  tv_button_text.setTextColor(textColor)

--

Now I need to set a Selector colour for enabled/disabled states. so I created this:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:color="@color/red" android:state_enabled="true" />
  <item android:color="@color/green" android:state_enabled="false" />
</selector>

and

  <CustomStatusButton
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="SAVE"
      android:textColor="@drawable/button_text_color_selector"
      />

But this doesn't set the colour depending on enable/disable. it only tastes the first colour which is red.

Am I missing something?

Upvotes: 1

Views: 1187

Answers (1)

Mkhytar Mkhoian
Mkhytar Mkhoian

Reputation: 66

The problem in this code

val textColor = typedArray.getColor(R.styleable.CustomStatusButton_android_textColor, Color.BLACK)

You try to get color but you need to get ColorStateList

Upvotes: 3

Related Questions