addFontWeightStyle NoSuchMethodException on TextView

  • A+
Category:Languages

I am using Androidx library for my project, And I want to set a font to textview, so when I am applying any font to any Textview components than system gives me

TypefaceCompatApi21Impl: java.lang.NoSuchMethodException java.lang.NoSuchMethodException: addFontWeightStyle [class java.lang.String, int, boolean]

this type of error in run time but app not getting crash.

So how to overcome this error.

Note: It will properly work on without android x dependency.

Below my code:

<androidx.appcompat.widget.AppCompatTextView         android:id="@+id/menu_tv_title"         style="@style/font_work_sans_medium"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:layout_gravity="center"         android:maxLines="1"         android:ellipsize="end"         android:paddingEnd="@dimen/_12sdp"         android:paddingStart="@dimen/_12sdp"         android:textColor="@android:color/black"         android:textSize="17sp"         android:gravity="center"         tools:text="title"         tools:visibility="gone"/> 

Here is the style

<style name="font_work_sans_medium" parent="@android:style/TextAppearance.Small">     <item name="android:fontFamily">@font/work_sans_medium</item> </style> 

I am also setting font by programatically like this

var typeFace: Typeface? = ResourcesCompat.getFont(context, R.font.work_sans_bold)     getTitleView().setTypeface(typeFace, Typeface.NORMAL) 

still getting this error

 


Try this:

 <style name="CutsomFontTextView" parent="Theme.AppCompat.Light.NoActionBar">     <!-- Customize your theme here. -->     <item name="android:fontFamily">@font/yourfont</item> </style> 

And apply this theme to textview like below

<androidx.appcompat.widget.AppCompatTextView     android:id="@+id/menu_tv_title"     android:theme="@style/CutsomFontTextView"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:layout_gravity="center"     android:maxLines="1"     android:ellipsize="end"     android:paddingEnd="@dimen/_12sdp"     android:paddingStart="@dimen/_12sdp"     android:textColor="@android:color/black"     android:textSize="17sp"     android:gravity="center"     tools:text="title"     tools:visibility="gone"/> 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: