48

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

Ankit Dubariya
  • 905
  • 2
  • 9
  • 22

9 Answers9

29

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'
Ankit Dubariya
  • 905
  • 2
  • 9
  • 22
  • 1
    Although @PerracoLabs mentioned the but has been reported, the version downgrade is the solution for now. – Dino Tw Mar 12 '19 at 06:45
17

The issue has been reported to the Android team and seems to be a fix in place for a future release:

As per comment #25 below version works

implementation 'androidx.appcompat:appcompat:1.1.0-alpha03'
implementation 'androidx.core:core-ktx:1.1.0-alpha05'
Prags
  • 2,457
  • 2
  • 21
  • 38
PerracoLabs
  • 16,449
  • 15
  • 74
  • 127
8

I delete this line from my xml file and now works without errors

android:textStyle="bold"

is about addFontWeightStyle Mehtod which throw a NoSuchMethodException in API21 Impl

Java bee
  • 2,522
  • 1
  • 12
  • 25
5

Just read the stack trace carefully - it's an issue with AndroidX. It seems that androidx.core.graphics.TypefaceCompatApi21Impl() attempts to resolve statically a method with signature addFontWeightStyle(lass java.lang.String, int, boolean) from some internal to the framework class android.graphics.FontFamily via reflection... and it turns out that method doesn't exist. I am having the same issue with androidx.appcompat.widget.AppCompatAutoCompleteTextView. Not much we can do until Google fixes it.

P.D.E.
  • 53
  • 4
3

Apparently it is happening when the method expected to be public (or if not existing, which I believe is not the case) and is not, according to Class.java method (look at the comment below):

private Method getMethod(String name, Class<?>[] parameterTypes, boolean recursivePublicMethods)
        throws NoSuchMethodException {
    if (name == null) {
        throw new NullPointerException("name == null");
    }
    if (parameterTypes == null) {
        parameterTypes = EmptyArray.CLASS;
    }
    for (Class<?> c : parameterTypes) {
        if (c == null) {
            throw new NoSuchMethodException("parameter type is null");
        }
    }
    Method result = recursivePublicMethods ? getPublicMethodRecursive(name, parameterTypes)
                                           : getDeclaredMethodInternal(name, parameterTypes);
    ***// Fail if we didn't find the method or it was expected to be public.***
    if (result == null ||
        (recursivePublicMethods && !Modifier.isPublic(result.getAccessFlags()))) {
        throw new NoSuchMethodException(name + " " + Arrays.toString(parameterTypes));
    }
    return result;
}

I believe it is some kind of a bug in Androidx.

lior_13
  • 577
  • 7
  • 18
2

New to coding here, but if you go into the toolbar via Build/'Edit Libraries and Dependencies' and click Suggestions, and click update against androidx.appcompat:appcompat:X.X.X, it will fix the error rather than doing it in the build.gradle file :)

JazzyJ
  • 331
  • 1
  • 2
  • 9
1

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"/>
Shahid Khan
  • 371
  • 2
  • 10
1

Your style parent is @android:style/TextAppearance.Small click on that . You will see only two item

<item name="textSize">14sp</item>
        <item name="textColor">?textColorSecondary</item>

So the error is correct . that style dont have fontFamily property inside it.

try changing your parent theme to

<style name="font_work_sans_medium" parent="Base.Theme.AppCompat.Light.DarkActionBar">
 <item name="android:fontFamily">@font/work_sans_medium</item>
</style>
Tejas Pandya
  • 3,987
  • 1
  • 26
  • 51
1

Have you tried to remove the last parameter when you set the typeface programatically?

getTitleView().setTypeface(typeFace)

instead of

getTitleView().setTypeface(typeFace, Typeface.NORMAL)

internally is using fontStyle

Franrc
  • 286
  • 3
  • 5