Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set hint of textinputlayout ALWAYS above edittext?

Tags:

java

android

How do I set the hint of TextInputlayout to be ALWAYS above EditText? That means I don't have to click the EditText to make it slide up. Right now, the hint of EditText and the hint of TextInputLayout are overlapping, I want them to be separated!

My current XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/relativeID"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/messageRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <android.support.design.widget.TextInputLayout
            android:id="@+id/inputLayout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"        
            android:hint="My Hint Text"
            app:hintEnabled="true"
            app:hintAnimationEnabled="false">

            <EditText
                android:hint="Type here"
                android:id="@+id/messageEditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                />

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

    </LinearLayout>

</RelativeLayout>

2 Answers

I just wanted to have the hint always on top too. Must say @Chirag has an important point, but I wanted an easy way for the endIcon functionality.

So very simple workaround: set a blank into the TextInputEditText e.g.

<com.google.android.material.textfield.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/hint_text" >

    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=" " />

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

When processing the input I make sure if the blank was deleted.

like image 102
Marcel Hofgesang Avatar answered Oct 23 '25 05:10

Marcel Hofgesang


The main functionality of the InputText Layout is floating edittext, password toggling and error visible.

So if you want hint above edittext then please take one textview and set edittext below it.

Don't use TextInput layout if you don't want the exact behaviour provided by it.

like image 32
Chirag Avatar answered Oct 23 '25 05:10

Chirag



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!