addFontWeightStyle NoSuchMethodException on TextView

Ankit Dubariya picture Ankit Dubariya · Feb 15, 2019 · Viewed 8k times · Source

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

Answer

Ankit Dubariya picture Ankit Dubariya · Feb 19, 2019

For some research i found the solution might me helpful, actually i am using alpha dependency which is not stable so i downgrade the lib version of AndroidX

I am using this dependency

 implementation 'androidx.core:core-ktx:1.1.0-alpha04'
 implementation 'androidx.appcompat:appcompat:1.1.0-alpha02'

you should use this instead of

implementation 'androidx.core:core-ktx:1.0.1'
implementation 'androidx.appcompat:appcompat:1.0.2'