Error inflating TextInputLayout when used with ViewPager

Looks like TextInputLayout just doesn’t work inside ViewPager.

The error is:

  • NoClassDefFoundError when running Instrumentation test with ant
  • How can i transform a Bitmap into a Uri?
  • Dual SIM card Android
  • How to change color of ListView items on focus and on click
  • Recyclerview with endless scrolling in nestedscrollview, triggers onScrolled
  • Streaming with Android MediaPlayer in SDK 8
  • Error inflating class android.support.design.widget.TextInputLayout

    Appcompat and Design library added. Theme is correct.

    I use PageAdapter to inflate and populate ViewPager.

    The TextInputLayout view inflates just fine in all other parts of the application.

    The layout I inflate for the ViewPager.

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="wrap_content">
    
      <android.support.design.widget.TextInputLayout
        android:id="@+id/text_input_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <EditText
          android:id="@+id/edit_text"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:hint="My Edit Text" />
    
      </android.support.design.widget.TextInputLayout>
    
    </LinearLayout>
    

    Related posts:

    Multiline EditText with Done SoftInput Action Label on 2.3
    How to Draw TextView on Canvas in android..?
    implement AsyncTask in Fragment android
    startActivityForResult() from a Fragment and finishing child Activity, doesn&#039;t call onActiv...
    Android Calendar get current day of week as string
    print `this` variable in android
  • Run-time permission of Marshmallow
  • Start video chat without user interaction - Android
  • Android flip front camera mirror flipped video
  • Show Download Manager progress inside activity
  • Remove Search View icon from hint text
  • Endless adapter for ViewPager
  • 4 Solutions collect form web for “Error inflating TextInputLayout when used with ViewPager”

    I tried and I could run as normally ( no exception )
    I used viewpager and custom adapter. that’s my

    activity_main.xml

     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    
    </RelativeLayout>
    

    and my Fragment layout ( I used RelativeLayout cause I’s gonna use fragment)

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <android.support.design.widget.TextInputLayout
                android:id="@+id/text_input_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    
                <EditText
                    android:id="@+id/edit_text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="My Edit Text" />
    
            </android.support.design.widget.TextInputLayout>
    
        </LinearLayout>
    
    </RelativeLayout>
    

    Also you know how to use viewpager adapter. if you dunno, you can commit rigth here.

    Use this

     <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.AppCompat">
    

    you have also to extend your class with AppCompatActivity instead of Activity

    You should try a few things to fix the problem.

    • extend your activity from AppCompatActivity
    • extend your (dialog)fragment from android.support.v4.app.Fragment.
    • use latest version of design library.
    • Instead of using EditText, use android.support.v7.widget.AppCompatEditText. For example:

          <android.support.design.widget.TextInputLayout
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          app:errorEnabled="true">
      
                          <android.support.v7.widget.AppCompatEditText
                              android:layout_width="match_parent"
                              android:layout_height="wrap_content"
                              android:hint="First Name"
                              android:inputType="textPersonName"
                              android:singleLine="true" />
      
          </android.support.design.widget.TextInputLayout>
      

    I had the same issue and none of the above solutions worked for me. Finally, I switched PagerAdapter to FragmentStatePagerAdapter and implemented the whole thing with Fragment.

    You can refer to this link for the reference.

    For my experiment, I tweaked the demo and inserted TextInputLayout inside it’s ViewPager and everything worked perfectly.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.