Material-components-android: [TextInputLayout] Prefix not aligned correctly when phone font size is changed

Created on 18 Nov 2019  路  11Comments  路  Source: material-components/material-components-android

Description: The prefix horizontal alignment is not correct when the font size of the phone is changed, it seems to be a bit higher from the text inside the TextInputEditText

Expected behavior: Should be aligned correctly the TextInputEditText

Android API version: Android 10

Material Library version: 1.2.0-alpha01

Device: Samsung Galaxy s10

bug

Most helpful comment

While the following solution is hacky as well, it works better than modifying the sizes. I'm adding this after the view is initialized.

      val prefixView = textInputLayout.findViewById<AppCompatTextView>(com.google.android.material.R.id.textinput_prefix_text)
      prefixView.layoutParams = LinearLayout.LayoutParams(
          ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT)
      prefixView.gravity = Gravity.CENTER

All 11 comments

Could you provide screenshots and the source code?

I realized that only the last 2 small sizes of the phone font only affect this bug

Screenshot

Screenshot_20191119-112301_Fibler

Source Code

<com.google.android.material.textfield.TextInputLayout
            android:id="@+id/loginPhoneInputLayout"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:hint="@string/phone_number"
            app:boxStrokeColor="@color/mtrl_textinput_default_box_stroke_color"
            app:hintTextColor="?attr/colorAccent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:prefixText="+1"
            app:prefixTextColor="@color/textPrimaryColor"
            app:startIconDrawable="@drawable/ic_phone_android_white_24dp">

        <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/phoneNumberEditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:backgroundTint="@color/transparent"
                android:imeOptions="actionDone"
                android:importantForAutofill="no"
                android:inputType="phone" />

</com.google.android.material.textfield.TextInputLayout>

@Edydaoud
Please set TextAppearance of prefixtext. hope this one help you.

app:prefixTextAppearance="@style/TextAppearance.AppCompat.Medium"

or set style look like this and set prefix text size

<style name="prefix" parent="Base.TextAppearance.AppCompat.Body2">
        <item name="android:textSize">13sp</item>
</style>

   app:prefixTextAppearance="@style/prefix"

It's also unaligned on my end.

Screen Shot 2020-02-14 at 9 54 44 AM

Even when setting TextAppearance

<com.google.android.material.textfield.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/mobile_number_hint"
    app:boxBackgroundColor="@color/colorSurface"
    app:boxStrokeColor="@color/colorOnSurface"
    app:hintTextColor="@color/colorOnSurface"
    app:prefixText="+1"
    app:prefixTextAppearance="@style/TextAppearance.App.Body1">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:maxLength="14"/>

</com.google.android.material.textfield.TextInputLayout>

Anything else we can do about this?

I also get this bug, the height is sometime automatically resized. And I fix it by adding height value.
For Dense Style, I give 54dp, and non Dense Style, I give 61dp. We hope it will be fixed next version release.

<com.google.android.material.textfield.TextInputLayout
    style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.Dense"
    android:layout_width="match_parent"
    android:layout_height="54dp"
    app:prefixText="Rp. "
>

I used 62dp to OutlinedBox.
I guess this small variation it's different by custom font.

Tried adding the height value, I am still having the issue.

While the following solution is hacky as well, it works better than modifying the sizes. I'm adding this after the view is initialized.

      val prefixView = textInputLayout.findViewById<AppCompatTextView>(com.google.android.material.R.id.textinput_prefix_text)
      prefixView.layoutParams = LinearLayout.LayoutParams(
          ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT)
      prefixView.gravity = Gravity.CENTER

@anishbajpai014 has a good solution!

While we are waiting for the fix to be released, @anishbajpai014's solution looks the best so far; but with a little tweak so that it looks safer and cleaner.

textInputLayout.prefixTextView.updateLayoutParams { 
    height = ViewGroup.LayoutParams.MATCH_PARENT
}
textInputLayout.prefixTextView.gravity = Gravity.CENTER

@mrehan27 your solution is good too, but requires android core ktx. It's not a problem, just a warning.

Was this page helpful?
0 / 5 - 0 ratings