Search code examples
androidmaterial-designandroid-textinputlayout

How can I add a resolve-action to the errortext in an TextInputLayout?


I have a text TextInputLayout which validates user input. In some situations I have a "auto-fix" possibility or would like to be able to ignore some warning and therefore would like to add an action to the message I output via setError(...).

I already tried with a ClickableSpan, but that did neither really work, because the click was not correctly handled, nor the usability seemed good, because the action text is too small to be easy to hit. Somehow I am looking for a "standard", material-style way to dynamically show an action button, maybe at the side of the TextView to have sufficient space, whenever the error is "auto-fixable".


Solution

  • Finally I decided that actions directly in the ErrorText are not that easy to handled and have chosen to use ImageButtons beside the TextInputLayout:

    <android.support.design.widget.TextInputLayout
        android:id="@+id/edit_activity_name_til"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:errorEnabled="true">
    
        <android.support.design.widget.TextInputEditText
            android:id="@+id/edit_activity_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/desc_activity_hint"
            android:inputType="textCapWords|textAutoCorrect"
            android:maxLines="1" />
    </android.support.design.widget.TextInputLayout>
    
    <ImageButton
        android:id="@+id/quickFixButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:elevation="2dp"
        android:visibility="gone"
        app:srcCompat="@drawable/ic_undelete" />
    

    And then in Java I show and hide that image button:

    if(error){
        mActivityNameTIL.setError(str);
        mQuickFixBtn1.setVisibility(View.VISIBLE);
    }else{
        mActivityNameTIL.setError("");
        mQuickFixBtn1.setVisibility(View.GONE);
    }