user8304564
user8304564

Reputation:

Set hint of textinputlayout ALWAYS above edittext?

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>

Upvotes: 4

Views: 3513

Answers (4)

Vin Norman
Vin Norman

Reputation: 3302

things have moved on since these answers, there's a simple property that will do exactly this.

 <com.google.android.material.textfield.TextInputLayout
       // your other properties
       app:expandedHintEnabled="false"
       // etc.

Upvotes: 0

Marcel Hofgesang
Marcel Hofgesang

Reputation: 1040

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.

Upvotes: 2

Deep Naik
Deep Naik

Reputation: 491

this will solve your problem

<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="wrap_content">

<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="vertical"
    android:padding="12dp">
    <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="My Hint Text"
                android:layout_marginTop="@dimen/activity_vertical_margin"
                app:hintTextAppearance="@android:style/TextAppearance.Small">

                <EditText
                    android:id="@+id/ed"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:enabled="false"
                    android:hint="Type here"
                    android:paddingTop="0dp"
                    android:textColor="@color/white"
                    android:textSize="20sp"
                   />

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

Upvotes: -2

Chirag
Chirag

Reputation: 56925

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.

Upvotes: 2

Related Questions