0

I've defined an attribute (and I set it in my theme)

<attr name="primaryColor" format="color|reference" /> 

how can I get the color in the code? I tried getResources().getColor(R.attr.primaryColor) but this fails...

2
  • check this stackoverflow.com/a/3441986/983741 Commented Nov 28, 2013 at 13:33
  • I dont't have an AttributeSet if I'm not in the constructor of a custom view... So this solution does not help me... I want to find out the color that is defined in the current theme... Anywhere, not only in the view Commented Nov 28, 2013 at 13:38

1 Answer 1

2

Try this:
Need a Context.

TypedValue typeValue = new TypedValue(); context.getTheme().resolveAttribute(R.attr.primaryColor, typeValue, true); if (typeValue.type >= TypedValue.TYPE_FIRST_COLOR_INT && typeValue.type <= TypedValue.TYPE_LAST_COLOR_INT) { // R.attr.primaryColor is a color int color = typeValue.data; } else { // R.attr.primaryColor is not a color } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.