Change the background color of a TextView on click

I need to change the background color of a TextView.

font color using ColorStateList, which I can change, but the background color does not accept ColorStateList

lblEtiqueta.setTextColor (new ColorStateList ( new int [] [] { new int [] {android.R.attr.state_pressed} new int [] {android.R.attr.state_focused} new int [0] }, new int [] { Color.rgb (255, 128, 192), Color.rgb (100, 200, 192), Color.White, } )); 

how to make background color?

The TextView control is created dynamically at runtime.

Thanks in advance.

+3
java android eclipse colors textview
Jan 07 2018-11-11T00:
source share
1 answer

You will need to set backgroundDrawable for the TextView. I just made my state lists in XML and it would be something like this:

 <?xml version="1.0" encoding="utf-8" ?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true"> <color android:color="#00ff00" /> </item> <!-- And so on --> </selector> 

From what I understand, from the documentation, if you want to make a list of states in Java code, you will need to use StateListDrawable

+2
Jan 07 2018-11-11T00:
source share



All Articles