I am having trouble scaling my text size based on the user’s device. I use constraint layout to constrain the TextViews. I found that autoTextSize could be used to automatically fit my text in the TextViews. However, when I run my code on an Emulator like BlueStacks, some texts are not scaled properly. Weirdly, some work but some do not, even though I am implementing it the same way.
This is an example of what it looks like: The register button is in a big font but other texts aren’t
This is what it looks like on my layout Editor:
My code for the register button:
<TextView
android:id="@+id/register"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="REGISTER"
app:autoSizeTextType="uniform"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="5:1"
app:layout_constraintEnd_toEndOf="@id/guideline3"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@id/guideline2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.541" />
my code for the "up to 8 characters" text.
<TextView
android:id="@+id/req"
android:layout_width="0dp"
android:layout_height="0dp"
android:autoSizeTextType="uniform"
android:text="*up to 8 characters"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="7:1"
app:layout_constraintEnd_toEndOf="@+id/guideline5"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/guideline4"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.538" />
Am I misusing autoSizeTextType somehow?
Also, my "Enter Username" text is very small. I use EditText and implement it the same way I did for the other two texts. If there is anyway I can make the font size bigger to fit the layout size, please let me know.
2
Answers
your
<TextView android:id="@+id/register"
is usingapp:autoSizeTextType="uniform"
attr,<TextView android:id="@+id/req"
haveandroid:autoSizeTextType="uniform"
– note the differenceapp
/android
. try to consolidate this param, you should probably useapp
in both/allTextView
snote that
setAutoSizeTextTypeWithDefaults
was introduced in API 26, so when you useandroid:
prefix then this attr will be respected only on 26+. using customapp:
makes this attr usable forTextView
(in factTextViewCompat
) on any OS versionin Android Studio above layout preview you have an option to switch "Device to preview" and also API version – try to change to some older (in this sample 25 at most, preferably lowest supported
minSdkVersion
) or newest OS version, IDE should catch and show differences in lookUse TextView params like this
You can check the official documentation for more info.