Keep Textinputlayout hint always top
Asked Answered
S

5

30

Would be possible have my hint text always top, as if it were focusable?

I tried this:

 <android.support.design.widget.TextInputLayout
            android:id="@+id/etSurnameInput"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/llNameImage"
            android:hint="My Hint Text"
            app:hintEnabled="true"
            app:hintAnimationEnabled="false"
            android:textColorHint="@color/gray_title">
Sarracenia answered 29/11, 2016 at 12:42 Comment(4)
you want it for all your fields?Gentile
what is the current behavior because this app:hintAnimationEnabled="false" should work ?Nevsa
@RushiAyyappa yes, i want to all my fieldsSealed
In that case use TextView.. Text input layout will animate up only if there is a focus on that field.Gentile
P
30

Actually, from com.google.android.material:material:1.3.0-alpha03 or higher, you can simply set app:expandedHintEnabled="false" to make it always float.

Picot answered 3/3, 2021 at 2:46 Comment(1)
great updated answer! thanks!Tear
N
5

Here's a hack - programmatically add a space in the text if it's empty. However, you would then have to remove the space if it's focused, else the user will start typing with a leading whitespace. Another option is to always trim the text typed by user.

Nutwood answered 20/10, 2017 at 20:20 Comment(0)
N
3

My final solution back then was to actually just set hintEnabled="false" & use a TextView on top.

Nutwood answered 22/5, 2018 at 7:41 Comment(0)
L
1

You can set android:minLines="2" to the TextInputEditText or greater than 2. Also keep android:gravity="top"

<com.google.android.material.textfield.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:gravity="top"
        android:hint="Username"
        android:minLines="2" />
Larva answered 12/1, 2021 at 23:17 Comment(0)
L
0

set android:textAlignment="center" to TextInputEditText and android:gravity="start" to TextInputLayout

<com.google.android.material.textfield.TextInputLayout
                android:id="@+id/tilName"
                app:expandedHintEnabled="false"
                android:hint="@string/name_hint"
                android:gravity="start"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <com.google.android.material.textfield.TextInputEditText
                    android:layout_gravity="center"
                    android:hint="@string/name_hint"
                    android:textAlignment="center"
                    android:id="@+id/fieldName"
                    android:inputType="text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    />
Lobation answered 5/5, 2021 at 7:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.