How to programmatically retrieve custom theme values ββvia Robolectric
I can access custom color theme values ββin my application via:
private int getTextColorFromTheme(int textAppearanceAttrResId, int state) {
TypedValue typedValue = new TypedValue();
>>>>/* missing TypedValue.data in Robolectric!! */
Resources.Theme theme = mContext.getTheme();
// textAppearanceAttrResId is a reference, so it is required to be retrieved this way.
theme.resolveAttribute(textAppearanceAttrResId, typedValue, true);
int[] textColorAttr = new int[]{android.R.attr.textColor};
int indexOfAttrTextColor = 0;
TypedArray a = mContext.obtainStyledAttributes(typedValue.data, textColorAttr);
ColorStateList csl = a.getColorStateList(indexOfAttrTextColor);
>>>>/* NPE thrown here in Robolectric! */
int color = csl.getColorForState(new int[]{state}, -1);
a.recycle();
return color;
}
But Robolectric throws NPE when trying to access ColorStateList. I noticed that mContext.getTheme () returns a theme with a TypedArray whose data is 0.
mContext is configured earlier in setup ():
ActivityController<MyActivity> activityController = Robolectric.buildActivity(MyActivity.class).attach();
MyActivity activity = activityController.get();
activity.setTheme(com.sudocoder.android.theme.R.style.Theme_Mine);
activityController.create().start().resume().visible();
mContext = activity;
+3
source to share
1 answer
We had huge problems getting certain resources to work with Robolectric in my current team. It looks like resources are of type and are either throwing a ResourceNotFound exception or returning an incorrect value (like zero or zero). Depending on your project setup, you can use Mockito to mock the theme, but beyond that I don't know if much will be done, maybe someone else will find out more.
0
source to share