1

I have a text view in Linear layout

<LinearLayout
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.8"
        android:orientation="vertical"
        android:id="@+id/textLayout"
        android:background="@drawable/bg"
        android:gravity="center"
        android:layout_gravity="center">
        <TextView
            android:id="@+id/detail_text"
            android:text="hello"
            android:background="#55000000"
            android:lineSpacingExtra="5dp"
            app:autoSizeMinTextSize="12sp"
            app:autoSizeMaxTextSize="100sp"
            app:autoSizeStepGranularity="2sp"
            app:autoSizeTextType="uniform"
            android:textColor="@android:color/white"
            android:layout_margin="10dp"
            android:layout_gravity="center"
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </TextView>
    </LinearLayout>

And in build.gradle I have

dependencies {
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support:cardview-v7:26.1.0'
    implementation 'com.android.support:recyclerview-v7:26.1.0'
    implementation 'com.google.android.gms:play-services-ads:11.4.2'
    implementation 'com.android.support:support-compat:26.0.0'
}

But It is showing a fixed size (minimum size ) text only. It is not auto resizing depending on phone width;

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
hard coder
  • 5,449
  • 6
  • 36
  • 61

3 Answers3

1

It is not recommended to use wrap_content for layout_height or layout_width as it might not work. You need to use fixed value for layout_height in your case.

From docs

If you set autosizing in an XML file, it is not recommended to use the value "wrap_content" for the layout_width or layout_height attributes of a TextView. It may produce unexpected results.

karan
  • 8,637
  • 3
  • 41
  • 78
0

You need to assign a fixed value to either layout_width or layout_height for auto text sizing property to work.

Vishal Arora
  • 2,524
  • 2
  • 11
  • 14
0

check this method:

TextViewCompat.setAutoSizeTextTypeWithDefaults(TextView textview, int autoSizeTextType)

it will look like this

TextViewCompat.setAutoSizeTextTypeWithDefaults(
        findViewById(R.id.detail_text), 
        TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM)

also from https://developer.android.com/guide/topics/ui/look-and-feel/autosizing-textview#setting-textview-autosize Using support library

<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"
      app:autoSizeMinTextSize="12sp"
      app:autoSizeMaxTextSize="100sp"
      app:autoSizeStepGranularity="2sp" />

</LinearLayout>
svkaka
  • 3,942
  • 2
  • 31
  • 55