How to get Textinputlayout hint android in multiple lines?
Asked Answered
S

3

14

I have tried many options available online but none of them seem to be working.

I have used this XML for this purpose.

<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:hintTextAppearance="@style/TextLabel">

    <com.projects.fonts.RobotoEditText
        style="@style/EditText"
        android:hint="@string/description"
        android:singleLine="false"
        app:font="@string/roboto_regular" />

</android.support.design.widget.TextInputLayout>

I have also set

<item name="android:singleLine">false</item>

in both TextLabel and EditText. But none of them are working.

Supersede answered 18/7, 2016 at 10:1 Comment(2)
Does RobotoEditText extend android.support.design.widget.TextInputEditText?Voiceless
@Voiceless Yes it does..Supersede
B
12

You can't.

TextInputLayout uses CollapsingTextLayout which measures the hint as a CharSequence:

mTextPaint.measureText(mTextToDraw, 0, mTextToDraw.length())

CharSequence has no notion of a line, thus you can't add more.


It's not quite a hint if it's so long. If you still want to display it consider adding a separate TextView below/above/over and animating it (if want/need to).

Bisset answered 6/7, 2017 at 8:54 Comment(0)
P
3

You can do this with TextInputLayout and TextInputEditText.

In the TextInputEditText set the hint as you normally would (android:hint="Hint Text").

In the TextInputLayout you must disable the hint by setting "app:hintEnabled="false".

Notes:

If you leave hintEnabled to true (true by default) the hint will be single line and turns into a label when the user taps the EditText giving it focus.

If you change hintEnabled to false this feature is removed and the EditText shows the hint with multiple lines as intended. It does not turn into a label and disappears once the user starts actually typing.

Example code below:

<com.google.android.material.textfield.TextInputLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:hintEnabled="false"
                    app:boxBackgroundMode="none">

                    <com.google.android.material.textfield.TextInputEditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="Your long hint"/>
                </com.google.android.material.textfield.TextInputLayout>
Paly answered 28/5, 2021 at 19:32 Comment(0)
K
0

In contrast to simas answer it seems that there is a workaround. You can achieve the expected behaviour by setting the hint programmatically in the TextInputEdittext. The downhill is that the floating label does not work after that, by I believe that it is not something that we expect with so long hint.

Important note: If we set the hint to the TextInputLayout, then it will not work.

Kid answered 1/9, 2019 at 7:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.