Search code examples
androidandroid-fragmentsandroid-textinputlayout

Cannot use TextInputLayout in Fragment


I cannot seem to be able to use TextInputLayout in fragment. Here is my code.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LoginFragment">

    <android.support.design.widget.TextInputLayout
        android:id="@+id/email_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/email"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="email"
            android:inputType="textEmailAddress"
            android:maxLines="1" />

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

</LinearLayout>

When I try to run I get this exception

Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.design.widget.TextInputLayout"

I red this post which says I should use AppCompat Theme and I am using AppCompat Theme

Here is my Theme

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

Solution

  • Adding this dependency to gradle solved the problem

    implementation 'com.android.support:design:27.1.1'