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