Android ColorStateList created programmatically and applied to TextColor

I am trying to apply a ColorStateList created using TextColor TextView code. The problem is that if I use the ColorStateList defined in xml, it works, but does not work, when I create the ColorStateList through the code.

This is how I create a ColorStateList

int[][] states = new int[][] { new int[] { android.R.attr.state_activated } };

int[] colors = new int[] { Color.parseColor("#FFFF00") };

myList = new ColorStateList(states, colors);

and I just applied it to TextView in this way

myTextView.setTextColor(myList);

and does not work. Using this xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_activated="true"  android:color="@color/yellow" />
   <item android:color="@color/black" />
</selector>

It works either to set the text color in xml, or by code in this way

myTextView.setTextColor(myTextView.getContext().getResources().getColorStateList(R.drawable.textcolor_selector));

I have been searching for a solution all over the Internet, but I really cannot find the cause of this problem, can someone help me?

Thank you

+4
source share
1 answer

, . state_activated:

int[][] states = new int[][] { new int[] { android.R.attr.state_activated }, new int[] { -android.R.attr.state_activated } };
int[] colors = new int[] { Color.parseColor("#FFFF00"), Color.BLACK };
myList = new ColorStateList(states, colors);
+2

Source: https://habr.com/ru/post/1525621/


All Articles