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

Time:11-19

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:gravity="start"
    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.

CodePudding user response:

you can set unit text size is dp instead of sp

android:textSize="15dp"

CodePudding user response:

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

  • Related