Typeface returns null on Android 4.4 when looking at widgets inside a parent view when targeting API higher than 21 -
there seems problem appcompat widgets on android.support.v7. basically, using getlayoutinflator().inflate()
load xml layout relativelayout
or linearlayout viewgroup
. then, pass viewgroup function extracts child views , attempts @ typeface of child view if textview, button or edittext. works fine on android 5 , above. however, on android devices running 4.4.4 or below, typeface of child view returns null. why widgets return null?
if inflate viewgroup, add parent activity , find widget id, can typeface.
i need change typeface of child view if textview, button or edittext. , code below works on android devices running 5 , above not below android 5.
public void checkview(view view) { // custom_typeface defined elsewhere in code. // if check of widgets below typeface gettypeface(), // returns null on android devices running 4.4.4 , below. if (view instanceof textview) { textview txt = (textview) view; typeface custom_typeface = createtypefacebasedonexistingstyle(txt.gettypeface); txt.settypeface(custom_typeface); } if (view instanceof edittext) { edittext edt = (edittext) view; typeface custom_typeface = createtypefacebasedonexistingstyle(txt.gettypeface); edt.settypeface(custom_typeface); } if (view instanceof button) { button btn = (button) view; typeface custom_typeface = createtypefacebasedonexistingstyle(txt.gettypeface); btn.settypeface(custom_typeface); } }
if has input or solution problem, appreciate it. thank in advance.
ok, figured out going on. seems when inflate xml layout , attempt typeface of widgets not have textstyle property defined in xml file on devices running android os @ 4.4.4 or below, typeface has not been defined , therefore it'll return null.
on android devices running 5.0 , above, widgets return default typeface no matter because textstyle defaults normal.
solution devices running android 4.4.4 , below define textstyle normal in xml file on widgets you'd change typeface. way, when xml layout inflated, textstyle defined , typeface created widget.
alternatively, in code, can check if typeface returns null , create new typeface normal text style.
hope make sense comes across same issue in future.
Comments
Post a Comment