Saturday, 15 February 2014

android - Why isn't the inflated viewGroup working? -



android - Why isn't the inflated viewGroup working? -

so basically, i'm trying alter font on views of xml layout. can done through non-xml approach. reason, setfont not work inflated view grouping child. think i'm going viewgroup wrong... how instantiate better? code works when utilize regular view button, dont want define 1000000 buttons , textviews solution create viewgroup created layout , iterate through views in alter font. ug please help!

public static void applyfonts(final view v, typeface fonttoset) { seek { if (v instanceof viewgroup) { viewgroup vg = (viewgroup) v; (int = 0; < vg.getchildcount(); i++) { view kid = vg.getchildat(i); applyfonts(child, fonttoset); } } else if (v instanceof textview) { ((textview) v).settypeface(fonttoset); } grab (exception e) { e.printstacktrace(); } } layoutinflater inflater = (layoutinflater) getsystemservice(context.layout_inflater_service); viewgroup vg = (viewgroup) inflater.inflate(r.layout.main, null); typeface rockwellfont = typeface.createfromasset(getassets(), "rockwell.ttf"); layoutinflater inflater = (layoutinflater) getsystemservice(context.layout_inflater_service); viewgroup vg = (viewgroup) inflater.inflate(r.layout.main, null); applyfonts(vg,rockwellfont); setcontentview(vg);

its improve extend view want have custom fonts this

package com.shahidawat.external; import com.shahidawat.r; import android.content.context; import android.graphics.typeface; import android.util.attributeset; import android.widget.textview; public class customtextview extends textview { public customtextview(context context) { super(context); init(context); } public customtextview(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); init(context); } public customtextview(context context, attributeset attrs) { super(context, attrs); init(context); } private void init(context context) { typeface type = typeface.createfromasset(context.getassets(), "fonts/monotypecorsiva.ttf"); settypeface(type); settextcolor(context.getresources().getcolor(r.color.black)); } }

then in xml can this

<com.shahidawat.external.customtextview android:id="@+id/txtheader" android:layout_width="match_parent" android:layout_height="30dp" android:layout_marginbottom="5dp" android:background="@drawable/separater" android:gravity="bottom|center_horizontal" android:text="header" android:textsize="20sp" android:textstyle="bold" />

android xml view fonts layout-inflater

No comments:

Post a Comment