How to get accent color programmatically?

Jakob picture Jakob · Dec 22, 2014 · Viewed 38.1k times · Source

How would one fetch the accent color set in styles, like below, programmatically?

    <item name="android:colorAccent">@color/material_green_500</item>

Answer

rciovati picture rciovati · Dec 22, 2014

You can fetch it from the current theme in this way:

private int fetchAccentColor() {
    TypedValue typedValue = new TypedValue();

    TypedArray a = mContext.obtainStyledAttributes(typedValue.data, new int[] { R.attr.colorAccent });
    int color = a.getColor(0, 0);

    a.recycle();

    return color;
}