简体   繁体   中英

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>

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.

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 bank into the TextInputEditText eg

<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.

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM