How to define ColorStateList for TextView?

Related searches

When my ListViewItem is highlighted, I want the text to turn white. How can I define this?

<selector xmlns:android="">
   <item android:state_focused="true" android:color="@color/testcolor1"/>
   <item android:state_pressed="true" android:state_enabled="false" android:color="@color/testcolor2" />
   <item android:state_enabled="false" android:color="@color/testcolor3" />
   <item android:color="@color/testcolor5"/>

Create file res/drawable/text_color.xml:

 <selector xmlns:android="">
    <item android:state_focused="true" android:state_pressed="false" android:color="#ffffff" />
    <item android:state_focused="true" android:state_pressed="true" android:color="#ffffff" />
    <item android:state_focused="false" android:state_pressed="true" android:color="#ffffff" />
    <item android:color="#000000" />

Then use @drawable/text_color from xml (or R.drawable.text_color from code) as text color for your list view items.

Color state list resource, Using ColorStateList for Textiew we can display colours when click on it. When we need to display a text as a link, and clicked text starts� Using ColorStateList for Textiew we can display colours when click on it. When we need to display a text as a link, and clicked on the text starts another activity or do some action.

In addition to what others have stated above, I would like to highlight one point, taken from the below url.

Note: The list of state specs will be matched against in the order that they appear in the XML file. For this reason, more-specific items should be placed earlier in the file. An item with no state spec is considered to match any set of states and is generally useful as a final item to be used as a default.

It's important that you have the broader condition towards the bottom in the selector tag. Hope this helps!

Apply ColorStateList for TextView to show when click on it, When my ListViewItem is highlighted, I want the text to turn white. How can I define this? <selector xmlns:android=" android">� How to define textAppearence value in android layout-Exceptionshub February 26, 2020 Android Leave a comment Questions: In my layout, I am using falling for predefined values for android:textAppearence.

Try this...

First, create a color state list text_color.xml placed in res/color directory.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="" xmlns:tools=""
  <item android:color="#000000" android:state_enabled="false"/>
  <item android:color="#FFFFFF"/>

Second, use

getColorStateList(@NonNull Context context,
            @ColorRes int id)

method to get color state list.

textView.setTextColor(ContextCompat.getColorStateList(context, R.color.text_color))

Third, enable(true) or disable(false) based on your requirements,

textView.isEnabled = true //when item is highlighted

Happy coding...

How to define ColorStateList for TextView?, Definition; Overloads; SetHintTextColor(Color); SetHintTextColor(ColorStateList); Applies to focussed, selected) of this TextView. ColorStateList colors); Color state list resource. A ColorStateList is an object you can define in XML that you can apply as a color, but will actually change colors, depending on the state of the View object to which it is applied. For example, a Button widget can exist in one of several different states (pressed, focused, or neither) and, using a color state list, you can provide a different color during each state.

TextView.SetHintTextColor Method (Android.Widget), @Override public void setColor(T view, ColorStateList colors) { view. Setting up the TextView of searchView suggestions drop down list for (TextView textView� If you want to set stateful color from code, you need to pass in ColorStateList as an argument to setTextColor passing an int to the method results in setting the color to all the states.

android.widget.TextView.setHintTextColor java code examples , Comment centrer le texte horizontalement et verticalement dans un TextView? Qu'est-ce que "Contexte" sur Android? Redémarrage de l'activité sur Android rotation Où puis-je placer le dossier 'assets' dans Android Studio? Différence entre la gravité et la mise en page de la gravité sur Android

val states = arrayOf(intArrayOf(android.R.attr.state_enabled)) val colors = intArrayOf(Color.RED) val colorStateList = ColorStateList(states, colors) ViewCompat.setBackgroundTintList(myView, colorStateList)

  • I'm not sure I understand what you mean?
  • Why did you posted the code? What it shows? (how do you use the drawable?)
  • Well I'm not sure how to change the textcolor. I assumed it would be in the selector code, but it's probably not. So I'm hoping someone can answer the question because I don't know.
  • Thanks! can you show me an example of what a @drawable/text_color might look like?
  • Somewhat like android:textColor="@drawable/text_color" on a TextView.
  • that should go in res/color
  • yes, and be referred to as @color/text_color. A @color can point to either a color or a ColorStateList.
  • If you set the text colour programmatically, call Context.getResources().getColorStateList(R.drawable.text_color) to get a ColorStateList, and pass that into the EditText.setTextColor method that takes a ColorStateList. Don't just pass the resource id in as an int (like I did...duh!).