Search code examples
androidandroid-support-libraryruntimeexceptionandroid-textinputlayout

RuntimeException when input exceeds counterMaxLength of TextInputLayout


I have a form with a TextInputLayout and TextInputEditText. This is the relevant XML:

<android.support.design.widget.TextInputLayout
        android:id="@+id/signup_til_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/activity_horizontal_margin"
        android:hint="Name"
        app:counterEnabled="true"
        app:counterMaxLength="16"
        app:errorEnabled="true">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/signup_etext_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="@style/TextAppearance.AppCompat.Small"/>
    </android.support.design.widget.TextInputLayout>

When I try to input the 17th character into this field my activity crashes with the following stacktrace:

java.lang.RuntimeException: Failed to resolve attribute at index 3
at android.content.res.TypedArray.twGetColorStateList(TypedArray.java:438)
at android.content.res.TypedArray.getColorStateList(TypedArray.java:420)
at android.widget.TextView.setTextAppearance(TextView.java:3029)
at android.support.design.widget.TextInputLayout.updateCounter(TextInputLayout.java:688)
at android.support.design.widget.TextInputLayout.access$300(TextInputLayout.java:84)
at android.support.design.widget.TextInputLayout$1.afterTextChanged(TextInputLayout.java:248)
at android.widget.TextView.sendAfterTextChanged(TextView.java:8929)

Apparently it has to do with not using the AppCompat Theme, but I am using the AppCompat theme already:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"/>

Not sure if this is relevant but I'm also observing this TextInputEditText using Jake Wharton's RxBinding library, RxTextView.textChanges(nameEditText);.


Solution

  • I suggest adding additional two attributes and giving it a style. one is app:counterTextAppearance and another is app:counterOverflowTextAppearance like here,

    <android.support.design.widget.TextInputLayout
      ....
      app:counterTextAppearance="@style/counterText"
      app:counterOverflowTextAppearance="@style/counterOverride">
    
    </android.support.design.widget.TextInputLayout>
    

    Those two styles are nothing but simply an item with android:textColor name like for example,

    <style name="counterText">
      <item name="android:textColor">#aa5353cc</item>
    </style>
    
    <style name="counterOverride">
      <item name="android:textColor">#ff0000</item>
    </style>
    

    See the full explanation here.

    If that didn't work then I suggest extending the Theme from Theme.Design.* as suggested in this answer.