androidandroid-layoutandroid-xml

Android - How to make the height of a TextInputEditText to be exactly of 2 lines?


I have a layout like this:

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/user_description_input_layout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="16dp"
        android:layout_marginTop="16dp"
        android:hint="Description"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:startIconContentDescription="Lalala">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/user_description_input"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="description" />

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

It's height is exactly the size of ONE line, but I wish it could be the size of 2 line exactly.

I tried adding theses attributes to my TextInputEditText tag:

<com.google.android.material.textfield.TextInputEditText
    ...
    android:maxLines="4"
    android:scrollbars="vertical" />

But that made it to start as a 1 line height and stop at 2 as the user types on it. I would like it to be fixed on 2 from the begining, even if it did not have enough text to need 2 lines.

I also would like it to have a fixed size and allow the user to scroll vertically in case he add some text that is larger than 2 lines.

I know I COULD do it programatically by adding enough caracters until it has a 2 lines height and fix this heigh size and then clean the TextInputEditText, but that is such an ugly solution.


Solution

  • Try this, in your TextInputEditText

    android:minLines="2"
    android:gravity="top"