title: Xamarin Forms Android Autosize Label TextCompat pre android 8 doesn't autosize text
body: <p>I want to utilise the auto-sizing feature of android textviews in my xamarin forms solution so that as the text length grows, the font sizes shrinks to never overflow the bounds of the label, and doesn't get truncated. I've created a custom Label control to do so and added an android custom renderer. It's not working in Android 7 and below. It is working in Android 8 and above.</p>
  <p>According to <a href="https://developer.android.com/guide/topics/ui/look-and-feel/autosizing-textview" rel="nofollow noreferrer">the docs</a> autosize support was introduced in android 8, but can be supported back to Android 4 with AppCompat.v4. However, my custom rendered label just renders the default font size in Android pre 8. It works fine in 8+ devices, the label text resizes as needed to not overflow the bounds. <a href="https://stackoverflow.com/a/45762382/2787772">The accepted answer to this question</a> with a similar issue on native android says it can be to do with not setting a width and height, I've tried setting widthrequest and heightrequest explicitly and it doesn't change anything. Also setting maxlines=1 doesn't change anything. An alternative thread suggests that custom fonts are the culprit. I created a vanilla forms solution using the default device font, and get the same effect.</p>
  <p>My code:</p>
  <pre><code>internal class AutosizeLabelRenderer : LabelRenderer
  {
      #region constructor
  
      public AutosizeLabelRenderer(Context context) : base(context)
      {
      }
  
      #endregion
  
      #region overridable
  
      protected override void OnElementChanged(ElementChangedEventArgs&lt;Label&gt; e)
      {
          base.OnElementChanged(e);
  
          if (e.NewElement == null || !(e.NewElement is AutoSizeLabel autoLabel) || Control == null) { return; }
  
          TextViewCompat.SetAutoSizeTextTypeUniformWithConfiguration(Control, autoLabel.AutoSizeMinTextSize,
                  autoLabel.AutoSizeMaxTextSize, autoLabel.AutoSizeStepGranularity, (int)ComplexUnitType.Sp);
          
      }
  
      #endregion
  }
  
  public class AutoSizeLabel : Label
  {
      public int AutoSizeMaxTextSize
      {
          get =&gt; (int)GetValue(AutoSizeMaxTextSizeProperty);
          set =&gt; SetValue(AutoSizeMaxTextSizeProperty, value);
      }
  
      public static readonly BindableProperty AutoSizeMaxTextSizeProperty = BindableProperty.Create(
          nameof(AutoSizeMaxTextSize),        // the name of the bindable property
          typeof(int),     // the bindable property type
          typeof(AutoSizeLabel));      // the default value for the property
  
      public int AutoSizeMinTextSize
      {
          get =&gt; (int)GetValue(AutoSizeMinTextSizeProperty);
          set =&gt; SetValue(AutoSizeMinTextSizeProperty, value);
      }
  
      public static readonly BindableProperty AutoSizeMinTextSizeProperty = BindableProperty.Create(
          nameof(AutoSizeMinTextSize),        // the name of the bindable property
          typeof(int),     // the bindable property type
          typeof(AutoSizeLabel));      // the default value for the property
  
  
      public int AutoSizeStepGranularity
      {
          get =&gt; (int)GetValue(AutoSizeStepGranularityProperty);
          set =&gt; SetValue(AutoSizeStepGranularityProperty, value);
      }
  
      public static readonly BindableProperty AutoSizeStepGranularityProperty = BindableProperty.Create(
          nameof(AutoSizeStepGranularity),        // the name of the bindable property
          typeof(int),     // the bindable property type
          typeof(AutoSizeLabel));      // the default value for the property
  
      //
  }
  </code></pre>
  <p><strong>Not working: Android 7 - text does not shrink</strong></p>
  <p><a href="https://i.stack.imgur.com/phuQC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/phuQC.png" alt="enter image description here" /></a></p>
  <p><strong>Working as expected: Android 8 and above</strong></p>
  <p><a href="https://i.stack.imgur.com/4Cigi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4Cigi.png" alt="enter image description here" /></a></p>
  <p>Xaml for above images:</p>
  <pre><code>        &lt;StackLayout HeightRequest=&quot;200&quot; WidthRequest=&quot;100&quot;&gt;
              &lt;Label Text=&quot;Fixed width and height, sentences get longer, text should shrink&quot; /&gt;
              &lt;controls:AutoSizeLabel
                  AutoSizeMaxTextSize=&quot;50&quot;
                  AutoSizeMinTextSize=&quot;8&quot;
                  AutoSizeStepGranularity=&quot;1&quot;
                  BackgroundColor=&quot;{StaticResource Shamrock}&quot;
                  HeightRequest=&quot;40&quot;
                  HorizontalOptions=&quot;Start&quot;
                  MaxLines=&quot;1&quot;
                  Text=&quot;A small sentence&quot;
                  WidthRequest=&quot;200&quot; /&gt;
              &lt;controls:AutoSizeLabel
                  AutoSizeMaxTextSize=&quot;50&quot;
                  AutoSizeMinTextSize=&quot;8&quot;
                  AutoSizeStepGranularity=&quot;1&quot;
                  BackgroundColor=&quot;{StaticResource Shamrock}&quot;
                  HeightRequest=&quot;40&quot;
                  HorizontalOptions=&quot;Start&quot;
                  MaxLines=&quot;1&quot;
                  Text=&quot;A larger sentence that shrinks&quot;
                  WidthRequest=&quot;200&quot; /&gt;
              &lt;controls:AutoSizeLabel
                  AutoSizeMaxTextSize=&quot;50&quot;
                  AutoSizeMinTextSize=&quot;8&quot;
                  AutoSizeStepGranularity=&quot;1&quot;
                  BackgroundColor=&quot;{StaticResource Shamrock}&quot;
                  HeightRequest=&quot;40&quot;
                  HorizontalOptions=&quot;Start&quot;
                  MaxLines=&quot;1&quot;
                  Text=&quot;An even larger sentence that shrinks more.&quot;
                  WidthRequest=&quot;200&quot; /&gt;
          &lt;/StackLayout&gt;
  </code></pre>