prom85
prom85

Reputation: 17858

get attribute programmatically (NOT in a view, but everywhere)

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

Upvotes: 0

Views: 379

Answers (1)

ramaral
ramaral

Reputation: 6179

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

}

Upvotes: 2

Related Questions