android - Why text is not bold when passing the style through custom view attributes? -


suppose have .xml style:

<style name="materialpreviewdetailstextsmall">     <item name="android:layout_marginleft">@dimen/material_margin</item>     <item name="android:textcolor">@color/material_preview_backgroud</item>     <item name="android:textsize">@dimen/material_normal_smal_text</item>     <item name="android:textstyle">bold</item> </style> 

i pass custom view through attributes:

<pl.valueadd.ledoc.view.viewpreviewrow                                  ...      app:view_title="@string/equipment_overview_id_label"               app:view_title_style="@style/materialpreviewdetailstextsmall"/> 

inside custom view obtain styled attributes , apply textview:

public class viewpreviewrow extends relativelayout {     textview tvtitle;      public viewpreviewrow(context context, attributeset attrs) {         super(context, attrs);         init(context, attrs);     }      private void init(context context, attributeset attrs) {         initview(context);         obtainstyledattributes(context, attrs);     }      private void obtainstyledattributes(context context, attributeset attrs)  {         typedarray attributes = context.obtainstyledattributes(attrs, r.styleable.viewpreviewrow);         charsequence text = attributes.getstring(r.styleable.viewpreviewrow_view_title);         if (text != null) {             tvtitle.settext(text);         } else {             throw new nullpointerexception("attribute view_title cannot null");         }         int appearance = attributes.getresourceid(r.styleable.viewpreviewrow_view_title_style, 0);         if (appearance != 0) {             settextviewappearance(tvtitle, appearance);             tvtitle.requestlayout();         }         ...         attributes.recycle();     } 

the question is:

why text has appropriate color, size, marginleft not bold?

i have found answer.

the expected style attributes can applied 1 one:

int appearance = attributes.getresourceid(r.styleable.viewpreviewrow_view_title_style, 0); applybold(context, attrs, appearance, tvtitle); applytextsize(context, attrs, appearance, tvtitle); applytextcolor(context, attrs, appearance, tvtitle);   private void applybold(context context, attributeset attrs, int style, textview textview) {     int[] textstyleattr = new int[]{android.r.attr.textstyle};     int indexofattrtextstyle = 0;     typedarray = context.obtainstyledattributes(style, textstyleattr);     int styleindex = a.getint(indexofattrtextstyle, -1);     a.recycle();     settypefacefromattrs(textview, styleindex); }  private void applytextsize(context context, attributeset attrs, int style, textview textview) {     int[] textstyleattr = new int[]{android.r.attr.textsize};     int indexofattrtextstyle = 0;     typedarray = context.obtainstyledattributes(style, textstyleattr);     int size = a.getdimensionpixelsize(indexofattrtextstyle, -1);     a.recycle();     textview.settextsize(typedvalue.complex_unit_px, size); }  private void applytextcolor(context context, attributeset attrs, int style, textview textview) {     int[] textstyleattr = new int[]{android.r.attr.textcolor};     int indexofattrtextstyle = 0;     typedarray = context.obtainstyledattributes(style, textstyleattr);     colorstatelist color = a.getcolorstatelist(indexofattrtextstyle);     if (color == null) {         color = colorstatelist.valueof(a.getcolor(indexofattrtextstyle, -1));     }     a.recycle();     textview.settextcolor(color); }  private void settypefacefromattrs(textview textview, int styleindex) {     textview.settypeface(typeface.default);     if (styleindex == 1) {         textview.settypeface(null, typeface.bold);     } else {         textview.settypeface(null, typeface.normal);     } } 

however solution inefficient. requires initialisation of typedarray every expected attribute (initialisation of memory & time comsuming task). however... works.


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -