105

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

    <item name="android:colorAccent">@color/material_green_500</item>
Ricardo A.
  • 685
  • 2
  • 8
  • 35
Jakob Harteg
  • 9,587
  • 15
  • 56
  • 78

9 Answers9

145

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;
}
rciovati
  • 27,603
  • 6
  • 82
  • 101
50

This also worked for me:

public static int getThemeAccentColor (final Context context) {
    final TypedValue value = new TypedValue ();
    context.getTheme ().resolveAttribute (R.attr.colorAccent, value, true);
    return value.data;
}
copolii
  • 14,208
  • 10
  • 51
  • 80
31
private static int getThemeAccentColor(Context context) {
    int colorAttr;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        colorAttr = android.R.attr.colorAccent;
    } else {
        //Get colorAccent defined for AppCompat
        colorAttr = context.getResources().getIdentifier("colorAccent", "attr", context.getPackageName());
    }
    TypedValue outValue = new TypedValue();
    context.getTheme().resolveAttribute(colorAttr, outValue, true);
    return outValue.data;
}
XYz Amos
  • 1,403
  • 14
  • 17
24

For those of you using Kotlin

@ColorInt
fun Context.themeColor(@AttrRes attrRes: Int): Int = TypedValue()
    .apply { theme.resolveAttribute (attrRes, this, true) }
    .data
Francis
  • 6,788
  • 5
  • 47
  • 64
Kevin
  • 1,405
  • 15
  • 16
12

I have an static method on a utils class to get the colors from the current theme. Most of times is colorPrimary, colorPrimaryDark and accentColor, but you can get a lot more.

@ColorInt
public static int getThemeColor
(
        @NonNull final Context context,
        @AttrRes final int attributeColor
)
{
    final TypedValue value = new TypedValue();
    context.getTheme ().resolveAttribute (attributeColor, value, true);
    return value.data;
}
Sotti
  • 14,089
  • 2
  • 50
  • 43
9

Here's my take on this:

public static String getThemeColorInHex(@NonNull Context context, @NonNull String colorName, @AttrRes int attribute) {
    TypedValue outValue = new TypedValue();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        context.getTheme().resolveAttribute(attribute, outValue, true);
    } else {
        // get color defined for AppCompat
        int appCompatAttribute = context.getResources().getIdentifier(colorName, "attr", context.getPackageName());
        context.getTheme().resolveAttribute(appCompatAttribute, outValue, true);
    }
    return String.format("#%06X", (0xFFFFFF & outValue.data));
}

Usage:

    String windowBackgroundHex = getThemeColorInHex(this, "windowBackground", android.R.attr.windowBackground);
    String primaryColorHex = getThemeColorInHex(this, "colorPrimary", R.attr.colorPrimary);
Esdras Lopez
  • 775
  • 12
  • 22
5

The MaterialColors could be used in this case if you want it to be single line

            MaterialColors.getColor(context, R.attr.colorAccent,context.getResources().getColor(R.color.fall_back_color));

The first argument is the context the second argument is the attribute you need to get and the third argument is the fallback color incase the attribute is missing or something goes wrong while getting the attribute color

Ismail Iqbal
  • 2,774
  • 1
  • 25
  • 46
  • 1
    One note. This will not work from service or application context until you set manually in App: applicationContext.setTheme(R.style.AppTheme). – Andoctorey Dec 09 '21 at 15:44
2

Kotlin solution:

    context.obtainStyledAttributes(TypedValue().data, intArrayOf(R.attr.colorAccent)).let {
        Log.d("AppLog", "color:${it.getColor(0, 0).toHexString()}")
        it.recycle()
    }
android developer
  • 114,585
  • 152
  • 739
  • 1,270
0

When you use Theme.Material3 you have to combine both solutions mentioned here because one works for R.attr and another one for android.R.attr.

@ColorInt
fun Context.getThemeColor(@AttrRes attrRes: Int): Int {
    val materialColor = MaterialColors.getColor(this, attrRes, Color.BLUE)
    if (materialColor< 0) return materialColor

    val resolvedAttr = TypedValue()
    theme.resolveAttribute(attrRes, resolvedAttr, true)
    val colorRes = resolvedAttr.run { if (resourceId != 0) resourceId else data }
    return ContextCompat.getColor(this, colorRes)
}
Andoctorey
  • 728
  • 9
  • 11
  • Shouldn't the `defaultValue` be something like `-1` instead of `Color.BLUE`? Also, the if condition should check if returned value is not `defaultValue`. e.g. `if (materialColor != defaultValue)` – rupinderjeet Feb 26 '22 at 06:10