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>
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.
Try changing to these versions. It worked for me.
compileSdkVersion 22
buildToolsVersion "22.0.1"
and
targetSdkVersion 22
add the following line to your build.gradle file:
compile 'com.android.support:design:25.3.1'
It works for me.
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
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.
Just replace android.support.design.widget.TextInputLayout
with com.google.android.material.textfield.TextInputLayout
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With