2

XML #1 - TextInputEditText inside TextInputLayout:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/textInputLayout"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    android:layout_width="0dp"
    android:layout_height="148dp"
    android:hint="Enter your email"
    app:boxStrokeColor="#000000"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/inputTextField"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="start|center_vertical" />

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

XML #2 - Only a simple TextInputEditText (not wrapped in TextInputLayout):

<com.google.android.material.textfield.TextInputEditText
    android:id="@+id/inputTextField"
    android:layout_width="0dp"
    android:layout_height="200dp"
    android:hint="Password"
    android:gravity="top"
    android:ellipsize="end"
    android:inputType="textMultiLine"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

All I want to do is change the gravity of the hint in the TextInputLayout to be start|top programmatically.

The 'programmatically' part is important because I'm reusing this TextInputLayout+TextInputEditText for multiple use-cases which require different positions of the hint text.

In case of XML #1, the hint stays there as the following 2 lines don't work:

Activity code:

    inputTextField.gravity = Gravity.START or Gravity.TOP
    textInputLayout.gravity = Gravity.START or Gravity.TOP

In case of XML #2, this works perfectly and the hint moves to the top:

Activity code:

    inputTextField.gravity = Gravity.START or Gravity.TOP

Why doesn't the hint move programmatically for XML #1?

Desired Result:

Desired result

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
Rohan Taneja
  • 9,687
  • 3
  • 36
  • 48

1 Answers1

3

In the layout you can use android:gravity="top" in the TextInputEditText:

<com.google.android.material.textfield.TextInputLayout
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    ...>

    <com.google.android.material.textfield.TextInputEditText
        android:gravity="top"
        ..>
    

Programmatically you can use:

textinputlayout.getEditText().setGravity(Gravity.TOP);

enter image description here

It requires at least the version 1.2.0-alpha06.

A final note.
With a FilledBox style currently it works only adding android:minLines to 2 or greater to the TextInputEditText.

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • Thanks! Upgrading to material 1.2.0-alpha05 did the trick! Never assumed that the issue could be in the library itself. Here's the fixed issue link on the material library repo: https://github.com/material-components/material-components-android/issues/933 – Rohan Taneja May 14 '20 at 08:13
  • It must also be set TextInputEditText inputType 'none' or 'textMultiLine' to achive the expected behaviour. – Taha Körkem Jul 06 '22 at 16:16