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;
}
public static int getThemeAccentColor (final Context context) {
final TypedValue value = new TypedValue ();
context.getTheme ().resolveAttribute (R.attr.colorAccent, value, true);
return value.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;
}
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