Search code examples
androidtextviewimageviewandroid-imageview

How to set tint for an text view programmatically in android?


I am trying to set the tintList programatically in the Android app for both imageView and textView. The code works for imageView but dosent work for textView. I have tried using the following code.

//Defining myColorStateList
myColorStateList = new ColorStateList(
                new int[][]{
                        new int[]{-android.R.attr.state_selected},
                        new int[]{android.R.attr.state_selected}
                },
                new int[]{
                        ContextCompat.getColor(mContext,R.color.red)
                        ContextCompat.getColor(mContext,R.color.blue)
                }
        );

//Works for imageView 
myImageView.setImageTintList(myColorStateList);

//The TextView picks up the tintList defined in xml, dosent apply the one being set programatically
myTextView.setCompoundDrawableTintList(myColorStateList);

What I have tried so far: In al cases the tintList defined in the myTextView resource file is being used, its not changed to the one being set programatically. I am looking to update the tint list for both imageView and the textView.

I am using the colorStateList here so that the views change colors when selected and unselected.

  1. myTextView.setCompoundDrawableTintList(null); - same result
  2. myTextView.setTextColor(null); - same result
  3. myTextView.setCompoundDrawableTintMode(PorterDuff.Mode.DST); - same result

Thanks for looking and your time.


Solution

  • myTextView.setTextColor(myColorStateList) 
    

    was the appropriate API to be used for the TextView in this case