Home » Android » android – Error inflating TextInputLayout when used with ViewPager

android – Error inflating TextInputLayout when used with ViewPager

Posted by: admin May 14, 2020 Leave a comment

Questions:

Looks like TextInputLayout just doesn’t work inside ViewPager.

The error is:

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>
How to&Answers:

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

Answer:

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>
    

Answer:

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.