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?

  • (Android) Proguard can't find dynamically referenced class javax.swing.*
  • Restart android machine
  • Google MapFragment is blank (white) with Could not find class 'maps.j.k'
  • How to configure the Http Cache when using Volley with OkHttp?
  • How Can i INTERCEPT an Incoming SMS With A specific Text
  • Increase the Android API level during app update
  • 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),
          Color.White
       }
    ));
    

    Change Background Color of TextView on Click

    Related posts:

    EditText inside ListView will not stay focused
    Gesture Recognition In Android
    how to use leakcanary, how to add leakcanary as a jar to build a apk with .mk file
    What is the difference between Android 4.4W (API20) and Android L (API20, L Preview) in the SDK Mana...
    Calling findViewById() from outside an activity
    Error :- Execution failed for task ':app:dexDebug' in android studio
  • How to compare the context objects of the two different Activities..?
  • Ui Automator 2.0 project in Android Studio
  • Voice recognition fails to work when the voice is under recording
  • New KitKat URIs dont respond to Intent.ACTION_VIEW
  • Eclipse and Android XML layouts gives “'default' is not a best match for any device/locale combination”
  • Android ViewPager setCurrentItem not working after onResume
  • 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);
        mText.setTextColor(colors);
    } catch (Exception e) {
        // handle exceptions
    }
    

    res/color/your_colors.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true"
              android:color="#222222"/>
        <item android:state_selected="true"
              android:color="#222222"/>
        <item android:state_focused="true"
              android:color="#222222"/>
        <item android:color="#0000ff"/>
    </selector>
    

    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.