Home > Enterprise >  How to autosize TextView in a ConstraintLayout without using autoSizeTextType?
How to autosize TextView in a ConstraintLayout without using autoSizeTextType?

Time:11-21

I have a TextView in my ConstraintLayout and I want the TextView to occupy only its parent width and height (0dp,0dp). I have seen that in small devices if in my ConstraintLayout I have an ImageView or a View with android:layout_width="0dp" android:layout_height="0dp" it will respect the constraints and don't be bigger than its parent. But in the case of the TextView it is not working the same as in small devices, although it has the same attributes than the ImageView, the text protrudes from its maximum space. To let you undestrand I am posting an image to let you see how the TextView is protruding out of its constraints:

Image of the problem: the text should show Example text

I am not using wrap content because I need the TextView to occupy exactly that squere of space. I think that the problem is that the TextView size doesn't auto-adjust automatically according to the screen size. What could I do so that the size is equal to a size that all the text is perfectly seen?

Also I ahve seen that I could use android:autoSizeTextType="uniform" but I can't as it is needed API level >=26

<TextView
    android:id="@ id/textView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:elevation="2dp"
    android:text="@string/example_of_text"
    android:textAppearance="@style/TextAppearance.AppCompat.Medium"
    android:textColor="@color/txt_light"
    android:textStyle="bold"
    app:layout_constraintBottom_toTopOf="@ id/view1"
    app:layout_constraintEnd_toStartOf="@id/view2"
    app:layout_constraintStart_toEndOf="@id/view3"
    app:layout_constraintTop_toBottomOf="@ id/view4" />

I tried to use android:layout_width="0dp" android:layout_height="0dp" in the TextView as I said using a ConstraintLayout but the text it is overlapping with its constraints.

Edit:

Changed TextView to AppCompatTextView:

 <androidx.appcompat.widget.AppCompatTextView
            android:id="@ id/textView"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:elevation="2dp"
            android:text="@string/example_of_text"
         android:textAppearance="@style/TextAppearance.AppCompat.Medium"
            android:textColor="@color/txt_light"
            android:textStyle="bold"
            app:layout_constraintBottom_toTopOf="@ id/view1"
            app:layout_constraintEnd_toStartOf="@id/view2"
            app:layout_constraintStart_toEndOf="@id/view3"
            app:layout_constraintTop_toBottomOf="@ id/view4"
            app:autoSizeTextType="uniform">

        </androidx.appcompat.widget.AppCompatTextView>

CodePudding user response:

You can set unit text size in dp instead of sp.

android:textSize="15dp"

CodePudding user response:

You can use AppCompatTextView for autosizing if your minSdk >= 14. According to the documentation:

The Support Library 26.0 provides full support to the autosizing TextView feature on devices running Android versions prior to Android 8.0 (API level 26). The library provides support to Android 4.0 (API level 14) and higher. The android.support.v4.widget package contains the TextViewCompat class to access features in a backward-compatible fashion.

You would specify android:autoSizeTextType="uniform". app:autoSizeTextType="uniform" may also work.

Update: Regarding use of AppCompatTextView in XML. From the documenation:

[AppCompatTextView] will automatically be used when you use TextView in your layouts and the top-level activity / dialog is provided by appcompat. You should only need to manually use this class when writing custom views.

CodePudding user response:

android:layout_marginStart="10dp" and Try to use ssp and sdp library. Give android:layout_width="0dp" as match_parent

  • Related