Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Element EditText is not allowed here" inside a TextInputLayout

Tags:

android

I'm trying to use the recent android.support.design.widget.TextInputLayout feature to get floating labels in Android.

By looking at the documentation it seems that you're supposed to put your EditText with the android:hint="myFloatingLabel" inside a android.support.design.widget.TextInputLayout element to benefit from it.

Functionally this works when I test it, but in Android Studio I get a warning on the EditText: "Element EditText is not allowed here" The consequence (besides the fact that I have annoying warnings) is that it breaks all code completion (for string resources, for id resources, etc).

Is it an Android Studio bug, or did I miss something?

My code sample for information:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <android.support.design.widget.TextInputLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/email"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:ems="10"
            android:hint="@string/email"
            android:inputType="textEmailAddress" />
    </android.support.design.widget.TextInputLayout>

</LinearLayout>
like image 774
Sir4ur0n Avatar asked Jun 09 '15 09:06

Sir4ur0n


6 Answers

Try using android.support.v7.widget.AppCompatEditText instead of EditText:

 <android.support.design.widget.TextInputLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.AppCompatEditText
        android:id="@+id/email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:ems="10"
        android:hint="@string/email"
        android:inputType="textEmailAddress" />
</android.support.design.widget.TextInputLayout>

I had the same issue and this did the job for me.

like image 174
kodlan Avatar answered Oct 20 '22 10:10

kodlan


Try changing to these versions. It worked for me.

compileSdkVersion 22
    buildToolsVersion "22.0.1"

and

targetSdkVersion 22
like image 37
Shadab K Avatar answered Oct 20 '22 10:10

Shadab K


add the following line to your build.gradle file:

compile 'com.android.support:design:25.3.1'

It works for me.

like image 45
T double Avatar answered Oct 20 '22 10:10

T double


Came across this old question while trying to find out how to get TextInputEditText inside TextInputLayout to be allowed and shown in the Android Studio layout preview.

The error "Not allowed here" disappeared after "running Invalidate Caches / Restart..." as mentioned by Frankie D

However, the preview still did not show up, until I changed the view to properly link to the design support lib as:

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

The new TextInputEditText has a few fixes and cool features as explained on https://www.journaldev.com/14748/android-textinputlayout-example

like image 44
sunadorer Avatar answered Oct 20 '22 08:10

sunadorer


I've just had this problem, not only on EditText but also on CheckBox and Button. Finally fixed it by running Invalidate Caches / Restart... Maybe some of the previous solutions also worked because they resulted in the caches being cleared.

like image 37
Frankie D Avatar answered Oct 20 '22 10:10

Frankie D


Just replace android.support.design.widget.TextInputLayout with com.google.android.material.textfield.TextInputLayout

like image 23
Vikram Kodag Avatar answered Oct 20 '22 10:10

Vikram Kodag