Error inflating TextInputLayout when used with ViewPager

Looks like TextInputLayout just doesn’t work inside ViewPager.

The error is:

  • Code Path per Flavor in Android Gradle
  • Android when to use ContentResolver applyBatch or BulkInsert
  • Listen Broadcast Before application uninstall
  • ADT 22.3 (android 4.4) - getting error “After scene creation, #init() must be called”
  • How to create always-top fullscreen overlay activity in Android
  • Android - R cannot be resolved to a variable
  • 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>
    

  • Android Appcompat v21 - use old spinner style
  • AVD won't launch (SDK Tools 22.6.2)
  • Making a interval timer in Java android
  • Zoom in to markers google map V2
  • Android SnackBar: error inflating SnackbarLayout
  • How to check programmatically if an application is installed or not in Android?
  • 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.