How to set TextColor using setTextColor(ColorsStateList colors)

I need to change text color when state change(pressed, focus)…

How to set text color of a TextView using ColorsStateList?

  • Creating a “favorite” feature on a list of questions that get randomized
  • Error: Could not access the Package Manager. Is the system running? - At Android Studio
  • Android: Is it possible to update a ImageView/ImageButton with a number to show the number of new messages?
  • How to display static google map on android imageview?
  • Choose between ArrayAdapter and SimpleAdapter
  • How to detect if changes were made in the preferences?
  • edit, solved:

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

    Change Background Color of TextView on Click

    Related posts:

    Is there way for extracting string resources in Android project in IntelliJ IDE?
    lock/unlock orientation
    CoordinatorLayout and AppBarLayout elevation
    Android dismiss keyboard
    How to resize image (Bitmap) to a given size?
    Bitmap recycle with largeHeap enabled
  • cannot be cast to
  • YouTube video playback with ExoPlayer
  • How to set the color of an Android ScrollView fading edge?
  • how to Show or Read docx file
  • Eclipse Upgrade Not Working
  • What is aapt(Android Asset Packaging Tool) & How it works?
  • 3 Solutions collect form web for “How to set TextColor using setTextColor(ColorsStateList colors)”

    If you need to set the colors in code (using ColorStateList), but still want to keep the color states in an XML, you might want to use this:

    try {
        XmlResourceParser parser = getResources().getXml(R.color.your_colors);
        ColorStateList colors = ColorStateList.createFromXml(getResources(), parser);
    } catch (Exception e) {
        // handle exceptions


    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="">
        <item android:state_pressed="true"
        <item android:state_selected="true"
        <item android:state_focused="true"
        <item android:color="#0000ff"/>

    You have to use getColorStateList()

    I was also struggling with this problem, if you want to use a state list, you need to declared it in the color resources folder, instead of the drawable folder, and use the setTextColor(getResources().getColorStateList(R.color.your_colors)).

    you can also use ContextCompat to load a color state list

    ColorStateList colors = ContextCompat.getColorStateList(this,R.color.my_color_list);
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.