Reputation: 355
I have made a custom Toolbar which works well but since I have move the source on a module, I am facing some problems:
Firstly : I can't retrieve the accent color because it provokes this crash :
Caused by: java.lang.NoClassDefFoundError: com.kassisdion.lib.R$attr
(This is how I have try to retrieve the accent color)
private static int getThemeAccentColor1(final Context context) {
TypedValue typedValue = new TypedValue();
TypedArray a = context.obtainStyledAttributes(typedValue.data, new int[]{R.attr.colorAccent});
int color = a.getColor(0, 0);
a.recycle();
return color;
}
public static int getThemeAccentColor2(final Context context) {
final TypedValue value = new TypedValue();
context.getTheme().resolveAttribute(R.attr.colorAccent, value, true);
return value.data;
}
Secondly : I can't access to android.support.v7.appcompat.R.attr.toolbarStyle value anymore (before I use to access to it statically).
I think the two issue are linked but I don't know what's wrong.
UPDATE :
I used to override a lot of Widget and I use this constructor :
public MyWidget(Context context, AttributeSet attrs) {
this(context, attrs, android.support.v7.appcompat.R.attr.myWidgetStyle);
}
Generally people replace android.support.v7.appcompat.R.attr.myWidgetStyle
by 0
but it can cause some issues (like the EditText becoming non editable).
After a lot of research, I've figured out that android.support.v7.appcompat.R.styleable.*
cannot be use. (On the last update, they have mad this field private, maybe it's linked to my problem).
So my solution was to create my own Toolbar instead of extending android.support.v7.widget.Toolbar
Upvotes: 2
Views: 3736
Reputation: 14399
To get a color from your theme:
@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;
}
To get the accent color for example (from a fragment):
final int color = UtilsColor.getThemeColor(getActivity(), R.attr.colorAccent);
Upvotes: 2