How would one fetch the accent color set in styles, like below, programmatically?
<item name="android:colorAccent">@color/material_green_500</item>
How would one fetch the accent color set in styles, like below, programmatically?
<item name="android:colorAccent">@color/material_green_500</item>
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;
}
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;
}
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;
}
For those of you using Kotlin
@ColorInt
fun Context.themeColor(@AttrRes attrRes: Int): Int = TypedValue()
.apply { theme.resolveAttribute (attrRes, this, true) }
.data
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;
}
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);
String.format()
helps to explain how to convert the negative integer value to a hex color string. –
Copulative 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
Kotlin solution:
context.obtainStyledAttributes(TypedValue().data, intArrayOf(R.attr.colorAccent)).let {
Log.d("AppLog", "color:${it.getColor(0, 0).toHexString()}")
it.recycle()
}
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)
}
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)
–
Valleau © 2022 - 2024 — McMap. All rights reserved.