You can retrieve the style
attribute(and pass it to the inner views) using:
attrs.getStyleAttribute()
or its equivalent(as the docs mention):
getAttributeResourceValue(null, "style")
Question
I am using a TextSwitcher with a TextView factory. I would like to pass the style I set on the TextSwitcher down to the TextViews.
TextSwitcher does not have a 3 arg constructor.
Is it possible to get the style attribute from the attribute set?
Xml
<com.my.TextSwitcher
style="@style/My.TextView.Style"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Java
public class MyTextSwitcher extends TextSwitcher {
public MyTextSwitcher(Context context, AttributeSet attrs) {
super(context, attrs);
int style = attrs.getAttributeIntValue("", "style", 0); // I tried this to no avail
setFactory(new MyTextViewFactory(context, attrs, style));
}
private static class MyTextViewFactory implements ViewFactory {
private final Context context;
private final AttributeSet attrs;
private final int style;
public MyTextViewFactory(Context context, AttributeSet attrs, int style) {
this.context = context;
this.attrs = attrs;
this.style = style;
}
@Override
public View makeView() {
return new TextView(context, attrs, style);
}
}
}
is the only answer to make my own custom attribute of int that will pass the style? I can't use the built in style tag?
Solution
You can retrieve the style
attribute(and pass it to the inner views) using:
attrs.getStyleAttribute()
or its equivalent(as the docs mention):
getAttributeResourceValue(null, "style")
OTHER TIPS
One alternative is to declare the TextView's in XML, however this gives less flexibility to the amount of child TextView's I can have.
<com.my.TextSwitcher
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/My.TextView.Style"
android:text="@string/some_text" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/My.TextView.Style"
android:text="@string/some_other_text" />
</com.my.TextSwitcher>