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...

prom85
  • 16,896
  • 17
  • 122
  • 242
  • check this http://stackoverflow.com/a/3441986/983741 – Sunil Mishra Nov 28 '13 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 – prom85 Nov 28 '13 at 13:38

1 Answers1

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

}
ramaral
  • 6,149
  • 4
  • 34
  • 57