Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

How to use TextView.SetAutoSizeTextTypeUniformWithConfiguration on Android 6.0.1?

JohnHardmanJohnHardman GBUniversity admin

In a custom renderer and in an effect, both on Android, I make use of TextView.SetAutoSizeTextTypeUniformWithConfiguration

On a Samsung Galaxy J5 running Android 6.0.1 this is resulting in a Java.Lang.NoSuchMethodError exception.

I believe that TextView.SetAutoSizeTextTypeUniformWithConfiguration was introduced in Oreo. However, according to https://android.jlelse.eu/auto-sizing-textviews-in-android-d615ee2dc794 , using the V26 (and I assume any later) support libraries should add support for TextView.SetAutoSizeTextTypeUniformWithConfiguration in Android 4.0 and later (I am interpreting the wording on that page, as the English is a little dodgy).

Exactly what is says is:

First, you have to add the support library v26 or more in your build.gradle file.
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support:support-v4:26.1.0'
Although, it has been introduced in Android Oreo, but through using support library, this feature is backward compatible upto the Android 4.0 and later.

In my Xamarin.Forms app, I add the following NuGets:
Xamarin.Android.Support.Compat 28.0.0.3
Xamarin.Android.Support.Media.Compat 28.0.0.3
Xamarin.Android.Support.v7.AppCompat 28.0.0.3

but still it reports Java.Lang.NoSuchMethodError on Android 6.0.1.

What else do I need to do to get that method supported?

Best Answer

Answers

  • JohnHardmanJohnHardman GBUniversity admin

    Thanks @Jarvan

    I was already using Xamarin.Android.Support.v4 but I hadn't realised that

    Control.SetAutoSizeTextTypeUniformWithConfiguration(10,10,10,10);
    

    needed to be replaced by

    TextViewCompat.SetAutoSizeTextTypeUniformWithConfiguration(Control, 10, 18, 10, (int) ComplexUnitType.Sp);
    

    Many thanks

  • JohnHardmanJohnHardman GBUniversity admin

    @Jarvan

    Ah, it seems that it doesn't work after all. It does build, but testing the following on my Samsung phone running Android 6.0.1

    Android.Support.V4.Widget.TextViewCompat.SetAutoSizeTextTypeUniformWithConfiguration(
        Control,
        1,
        (int)(Device.GetNamedSize(NamedSize.Large, typeof(Label)) * 2.0),
        1,
        (int)ComplexUnitType.Sp);
    

    does not have the same result as running the following on a Motorola running Android 9

    Control.SetAutoSizeTextTypeUniformWithConfiguration(
        1,
        (int)(Device.GetNamedSize(NamedSize.Large, typeof(Label)) * 2.0),
        1,
        (int)ComplexUnitType.Sp);
    

    The Motorola running Android 9 does what I would expect, the Samsung running 6.0.1 does not.

  • clevertreeclevertree Member ✭✭
    edited July 12

    @Jarvan did you get any further with this? Android 7 and downwards isn't working properly - the font appears too small for me. See full description in my SO question

  • SensGefrSensGefr Member

    @clevertree I saw your SO question regarding this subject. Do you think it is possible to do the same with the ButtonRenderer?

    I have a custom renderer that goes like:

    class AutoFitButtonRenderer : ButtonRenderer
        {
            public AutoFitButtonRenderer(Context context) : base(context)
            {
    
            }
            protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
            {
                base.OnElementChanged(e);
    
                if (e.NewElement != null)
                {
                    if (Control == null)
                    {
                        SetNativeControl(CreateNativeControl());
                    }
                    Control.SetMaxLines(1);
                    Control.SetLines(1);
    
                    TextViewCompat.SetAutoSizeTextTypeUniformWithConfiguration(
                                Control,
                                button.MinAutoFitFontSize, 
                                button.MaxAutoFitFontSize, 
                                1, 
                                (int)ComplexUnitType.Sp);
                }
                Control.SetAllCaps(false);
            }
    }
    
    

    which does it's job for Android >= 8, but fails for previous Android versions.

    I find difficul to implement what you did to solve this issue in this specific case. Any ideas?

    Thanks!

Sign In or Register to comment.