TextView autoSizeTextType not working in Compat
Asked Answered
U

5

13

I tried using autoSizeTextType. My minSdk is 24, all the tools are 26 and compat ist 26-beta2 as well.

Making them adjustable was tried through code:

dialogWeight.touchables.filterIsInstance<Button>().forEach {
TextViewCompat.setAutoSizeTextTypeWithDefaults(it, 
TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM) }

And xml:

<android.support.v7.widget.AppCompatTextView
android:layout_height="match_parent"
android:text="7"
android:autoSizeTextType="uniform"/>

Any ideas ? I'm starting to believe that it's currently bugged

Unavoidable answered 12/7, 2017 at 13:42 Comment(0)
U
19

Okay, so the combination of settings that worked :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<android.support.v7.widget.AppCompatTextView
android:text="7"
app:autoSizeTextType="uniform"/>

You also need the appcompat-v7 library as a dependency in your module build.gradle file.

dependencies {
    implementation 'com.android.support:appcompat-v7:27.1.1'
}
Unavoidable answered 12/7, 2017 at 13:51 Comment(4)
Could you also please post the relevant contents of your build.gradle that includes the compatibility bits as I'm having trouble getting this working. Also, does the Android Studio editor show any error on the app:autoSizeTextType="uniform" line? And are you able to address the AppCompatTextView as a TextView programatically?Tauto
I also have the same issue. To use the AppCompatTextView and TextViewCompat properly, you should include support-v4 (com.android.support:support-v4:27.0.1) and appcompat-v7 (com.android.support:appcompat-v7:27.0.1) to your build gradle. Make sure to change your <TextView/> xml tag into <android.support.v7.widget.AppCompatTextView/>. Hope It can be any help for you :)Yankeeism
@Yankeeism appcompat-v7 installs support-v4 as a dependency. You only need appcompat-v7 in your build.gradle file.Hausa
Side note, make sure not to set android:singleLine="true", then the auto-shrink will not work. Also set autoSizeMinTextSize, otherwise a default value seems to be applied and the text could be cut off at the end.Mauretta
R
12

The key thing to understand is to use app:autoSizeTextType, as opposed to android:autoSizeTextType

Per the documentation:

To define the default setting in XML through the support library, use the app namespace and set the autoSizeTextType attribute to none or uniform.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <TextView
    android:layout_width="match_parent"
    android:layout_height="200dp"
    app:autoSizeTextType="uniform" />

</LinearLayout>
Revile answered 21/11, 2018 at 16:51 Comment(0)
C
9

I solved it programmatically.

TextView number1 = findViewById(R.id.number_one);
TextViewCompat.setAutoSizeTextTypeWithDefaults(number1, TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);

and the XML:

  <TextView
        android:id="@+id/number_one"
        android:autoSizeTextType="uniform"
        android:gravity="center"
        android:text="1"  />
Calloway answered 12/10, 2017 at 15:36 Comment(0)
I
4

I wanted to update this thread since android.support.v7 has been migrated to androidX

I was having problems getting the text to display at all and what fixed it for me was this note about singleLine in one of the comments:

Side note, make sure not to set android:singleLine="true", then the auto-shrink will not work.

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/tempText"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:maxLines="1"
        android:textColor="@color/white"
        android:textSize="60dp"
        app:autoSizeMinTextSize="20dp"
        app:autoSizeTextType="uniform"
        tools:text="99&#xb0;" />
Incisive answered 27/6, 2020 at 15:22 Comment(0)
M
0

These days with androidx you want to use implementation "androidx.legacy:legacy-support-v4:1.0.0" along with TewxtView and e.g. android:autoSizeTextType="uniform"

Mannequin answered 9/9, 2021 at 1:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.