Current answers do not help, this question does not answer
I created a library with a custom view that inflates the layout upon creation. Views in the layout are styled using style="?attr/labelStyle" or any other attribute.
The attribute is declared by the attrs.xml library:
<attr name="myViewStyle" format="reference"/> <declare-styleable name="MyView"> <attr name="labelStyle" format="reference|color"/> </declare-styleable>
I set the default value for this attribute in the styles.xml library:
<style name="MyViewStyle"> <item name="labelStyle">@style/LabelStyle</item> </style> <style name="LabelStyle"> <item name="android:textColor">?android:attr/textColorPrimary</item> <item name="...">...</item> </style>
And finally, in the themes.xml library:
<style name="MyViewStyleLight" parent="Theme.AppCompat.Light"> <item name="myViewStyle">@style/MyViewStyle</item> </style>
Now these were the default library styles, but they were overridden in the main styles.xml project
<style name="AppTheme" parent="Theme.AppCompat.Light"> <item name="myViewStyle">@style/MyViewStyleCustom</item> </style> <style name="MyViewStyleCustom" parent="MyViewStyleLight"> <item name="android:textColor">@color/gray</item> <item name="...">...</item> </style>
Custom view code:
public MyView(Context context) { this(context, null, R.attr.myViewStyle, 0); } public MyView(Context context, @Nullable AttributeSet attrs) { this(context, attrs, R.attr.myViewStyle, 0); } public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { this(context, attrs, defStyleAttr, 0); } public MyView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(createThemeWrapper(context, R.attr.myViewStyle, R.style.MyViewStyleLight), attrs, defStyleAttr, defStyleRes); initLayout(); } private static Context createThemeWrapper(Context context, int styleAttr, int defaultStyle) { final TypedArray ta = context.obtainStyledAttributes(new int[]{styleAttr}); int style = ta.getResourceId(0, defaultStyle); ta.recycle(); return new ContextThemeWrapper(context, style); } private void initLayout() { LayoutInflater inflater = LayoutInflater.from(getContext()); inflater.inflate(R.layout.my_view, this); ... }
I will talk about ContextThemeWrapper below. Now the application crashes on the line where the layout swells. Here is an important part of the crash log:
android.view.InflateException: Binary XML file line
Layout expansion cannot find attribute value. When I tried to get the attribute by code, it returns nothing. The attribute really exists, only it does not have the value set for it, although I clearly set it.
How exactly should I create my library? I'm pretty sure that I did the same as the SublimePicker library , but that just won't work. There's a slight difference in part with ContextThemeWrapper, but that is probably not a problem. I feel like I forgot a tiny thing somewhere that makes the attribute inappropriate, something is not connected, I donβt know.
I know this is a very long question, but it cannot be more concise, I simplified everything as much as I could. I changed most of the information that was in the previous version of my question, making it completely different. These two answers are not relevant right now, not what they have ever been. The reward was automatically rewarded.
If this can help someone, I can add the download to my actual project, but as I said, this simplified example has the same form as my project.