0

I have a ConstraintLayout with some elements including an AppCompatTextView. Also, I am using app:autoSizeTextType="uniform" in the AppCompatTextView to resize it according to the screen size (so, to make this work the AppCompatTextView width and height is 0dp (match_parent)) . The problem is that to make the text look a normal size in a 5" and 6" devices, I have added a layout_marginTop and a layout_marginBottom to make the layout of the AppCompatTextView smaller so that the AppCompatTextView size is resizing to a smaller size as its layout is smaller. The problem is that in small devices like 3.7" or 4" the AppCompatTextView gets cropped because of its layout because of the layout_margin is so big for that screen density that the text doesn't fit.

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/tev"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:gravity="start"
        android:text="@string/strng"
        app:autoSizeTextType="uniform"
        app:layout_constraintBottom_toBottomOf="@id/dr2"
        app:layout_constraintEnd_toStartOf="@id/dlo"
        app:layout_constraintStart_toEndOf="@id/miu"
        app:layout_constraintTop_toTopOf="@id/cra"
        android:layout_marginTop="12dp"
        android:layout_marginBottom="12dp"
        />

See how the AppCompatTextView crops (The text is "Example TextView"): enter image description here

I have seen that a possible solution could be to set app:autoSizeMinTextSize to a small dp, but I have thought that it could be a better solution to set a dynamic layout margin according to the screen size so that the layout margin adapts to the screen size, could that be a good solution?

ADM
  • 20,406
  • 11
  • 52
  • 83
sacacorchos
  • 163
  • 9
  • You can set different margin sizes based upon screen size and other device attributes. See [AlternativeResources](https://developer.android.com/guide/topics/resources/providing-resources#AlternativeResources). for details. – Cheticamp Dec 04 '22 at 16:30

1 Answers1

0

Its hard to know what would work for you since we only see a portion of your code/window, but I think this could work

Option A: specify the min, max and granularity of the autoSize, so you can control how the text changes

android:autoSizeMinTextSize="12sp"
android:autoSizeMaxTextSize="100sp"
android:autoSizeStepGranularity="2sp"

Option B: create a set of sizes in resources and use android:autoSizePresetSizes="@array/autosize_text_sizes"

In res/values/arrays.xml create

<resources>
  <array name="autosize_text_sizes">
    <item>10sp</item>
    <item>12sp</item>
    <item>20sp</item>
    <item>40sp</item>
    <item>100sp</item>
  </array>
</resources>