How can I set the color of my text to "textColorSecondary" programmatically? I've tried the code below but it doesn't work. Does anyone know what's wrong with the code?
TextView tv1 = ((TextView)v.findViewById(R.id.hello_world));
tv1.setTextColor(Color.textColorSecondary);
Edit:
To get color from attribute use this:
TypedValue typedValue = new TypedValue();
Theme theme = context.getTheme();
theme.resolveAttribute(R.attr.textColorSecondary, typedValue, true);
int color = typedValue.data;
What only really worked for me is this implementation:
int textColor = getTextColor(context, android.R.attr.textColorSecondary);
public int getTextColor(Context context, int attrId) {
TypedArray typedArray = context.getTheme().obtainStyledAttributes(new int[] { attrId });
int textColor = typedArray.getColor(0, 0);
typedArray.recycle();
return textColor;
}
The other solutions here returned wrong color IDs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With