Search code examples
androidxamarin.androidtextviewandroid-support-libraryautoresize

Api 26 AutoResizeTextView Not Working


I have a TextView, inside a number of layouts.
This View is an item inside a RecyclerView, and the texxt is set programmatically inside the Adapter.

Sometimes the text is too big and gets cut off. I wanted therefore to use the AutoResize feature in Api 26. However, it doesn't work, even though I updated the Support Library, and put in the xml attributes.

This is my code:

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:cardview="http://schemas.android.com/apk/res-auto"
    android:id="@+id/zmanCard"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_gravity="center_horizontal"
    android:gravity="center_horizontal"
    cardview:cardUseCompatPadding="false"
    cardview:cardPreventCornerOverlap="false"
    cardview:cardCornerRadius="4dp"
    cardview:cardElevation="2dp">
  <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="?attr/colorPrimary"
      android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:layout_gravity="top"
        android:orientation="vertical"
        android:background="?attr/colorPrimaryDark">
     <!-- The AutoResizeTextView -->
      <android.support.v7.widget.AppCompatTextView
          android:layout_width="match_parent"
          android:layout_height="20dp"
          android:id="@+id/zmanCardTitle"
          android:textColor="#ffffff"
          cardview:autoSizeTextType="uniform" 
          cardview:autoSizeMaxTextSize="13sp"
          cardview:autoSizeMinTextSize="5sp"
          cardview:autoSizeStepGranularity="1sp"
          android:gravity="center"
          android:singleLine="true"
          android:ellipsize="none"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
      <ImageView
          android:layout_width="24dp"
          android:layout_height="wrap_content"
          android:layout_weight="0.5"
          android:layout_marginTop="2dp"
          android:layout_marginLeft="2dp"
          android:layout_marginRight="2dp"
          android:alpha="0.8"
          android:id="@+id/zmanCardImage" />
      <FrameLayout
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:layout_marginTop="7dp"
          android:layout_marginLeft="0dp"
          android:layout_marginRight="2dp">
        <TextView
            android:text="5:40"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="2dp"
            android:id="@+id/zmanCardTime"
            android:textColor="#ffffff"
            android:ellipsize="none"
            android:textSize="18sp"
            android:singleLine="true"/>
        <ProgressBar
            android:id="@+id/zmanProgressBar"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_gravity="center"
            style="@style/Base.Widget.AppCompat.ProgressBar" />
      </FrameLayout>
    </LinearLayout>
  </LinearLayout>
</android.support.v7.widget.CardView>

Solution

  • Delete this attribute from your TextView:

    android:singleLine="true"
    

    And replace it with:

    android:maxLines="1"
    

    Note also that the amount of scaling that the auto-sizing framework can apply is limited by both the width and the height of your TextView. You've set a height of 20dp, so the text won't be able to scale up very far.