1

I´m trying to outsource my custom view into an own library.

Is it possible to get the colorPrimary, colorPrimaryDark and colorAccent attributes outside the scope, maybe at runtime?

Solution
I´ve pointed it out myself.

In xml use:

?attr/attributeName

f.e.

?attr/colorAccent

In code use sth like:

public int getColorValueByAttr(Context context, int attrResId) {
    int color = 0;

    TypedArray a = null;
    try {
        int[] attrs = {attrResId};
        a = context.obtainStyledAttributes(attrs);
        color = a.getColor(0, 0);
    } finally {
        if (a != null) {
            a.recycle();
        }
    }

    return color;
}
monty.py
  • 2,511
  • 1
  • 17
  • 29

0 Answers0