Library's style attributes have no values even though they are explicitly set
This answer is based on what I understand from your question and conversation between you and Vinayak B. If I misinterpret ,Please correct me.
there is difference in style.xml in both place app as well as lib. addition I have removed theme.xml as well as changes in constructor of MyView.java for default style
I have changed following things
overridden in the main project styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light"> <item name="myViewStyle">@style/MyViewStyleCustom</item> </style> <style name="MyViewStyleCustom" parent="MyViewStyle"> <item name="labelStyle">@style/LabelStyle123</item> </style> <style name="LabelStyle123"> <item name="android:textColor">#f00</item> </style>
lib styles.xml
<resources> <style name="MyViewStyle"> <item name="labelStyle">@style/LabelStyle</item> <item name="TextStyle">@style/textStyle</item> </style> <style name="LabelStyle"> <item name="android:textColor">#00f</item> </style> <style name="textStyle"> <item name="android:textColor">#009</item> </style> </resources>
MyView.java - changed constructor of and set default MyViewStyle if no any attribute come from application.
public MyView(Context context) { this(context, null, R.attr.myViewStyle, R.style.MyViewStyle); } public MyView(Context context, @Nullable AttributeSet attrs) { this(context, attrs, R.attr.myViewStyle, R.style.MyViewStyle); } public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { this(context, attrs, defStyleAttr, R.style.MyViewStyle); } public MyView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(createThemeWrapper(context, defStyleAttr,defStyleRes), attrs, defStyleAttr, defStyleRes); initLayout(); } private static Context createThemeWrapper(Context context, int styleAttr, int defaultStyle) { final TypedArray ta = context.obtainStyledAttributes(new int[]{styleAttr}); int style1 = ta.getResourceId(0, defaultStyle); ta.recycle(); return new ContextThemeWrapper(context, style1); }
so either it will take default labelStyle if it is not overridden in main activity style or overridden labelStyle
This answer is based on what I understand from your question. If I misinterpret ,Please correct me.
First of all myTextColor
is an attribute name in your library. not a attribute value. You supposed to give a value for myTextColor
when ever you using this library. Otherwise there may occur 'InflateException'
. You can avoid this by following way.
<YourCustomeView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:myTextColor="#000"/>
1. Set myTextColor
value directly when you use outside the library.
OR
In your library where you using this
myTextColor
attribute, check if this attribute have value or not. If it doesn't have any value then use a default value formyTextColor
private void init(@Nullable AttributeSet attrs) { TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.MyLibrary); boolean hasRawRes = ta.hasValue(R.styleable.myTextColor); if(hasRawRes){ // Use `myTextColor` attr here }else{ // use default color }
}
UPDATE ANSWER
This answer for the updated question
First of all you are trying to fetch a attr
value from your library to your project using ?attr/
.Which does not going to work. because
Your project using Theme.AppCompat
theme as (I'm guessing) parent theme for your Activities. When you use ?attr
inside that activity, you can only fetch attribute values of Theme.AppCompat
. But you are trying to fetch ?attr/labelStyle
which is not a attribute of Theme.AppCompat
rather than it's your library attribute. That's why you are getting that crash. If you want to use any style from your library to your project you can use @style
tag
For example
style="@style/labelStyle"
If it's not what you are looking for ,Please kindly share your source code.So I can understand more on this problem.