Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set text color to secondary color programmatically

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);
like image 784
wbk727 Avatar asked Dec 06 '25 16:12

wbk727


2 Answers

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;
like image 166
Anton Kovalyov Avatar answered Dec 09 '25 04:12

Anton Kovalyov


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.

like image 29
petrsyn Avatar answered Dec 09 '25 05:12

petrsyn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!