Search code examples
androidandroid-xmlandroid-styles

How to get accent color programmatically?


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

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

Solution

  • 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;
    }