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
source
share