Create a gist now

Instantly share code, notes, and snippets.

TextInputLayout Issues with setError

This is a gist that was built for my post on TextInputLayouts here: http://w3bshark.com/blog/input-layouts

It is an example of how making back-to-back calls to the setError() functionality for the same text input layout will cause the text input layout to only display the error that was set originally instead of the final error that was set.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main">
<android.support.design.widget.TextInputLayout
android:id="@+id/first_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/text_input_one_hint"
app:errorEnabled="true" >
<android.support.design.widget.TextInputEditText
android:id="@+id/first_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:imeOptions="actionNext"
android:nextFocusDown="@+id/second_edit_text"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/second_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/first_input_layout"
android:hint="@string/text_input_two_hint"
app:errorEnabled="true" >
<android.support.design.widget.TextInputEditText
android:id="@+id/second_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:imeOptions="actionDone"/>
</android.support.design.widget.TextInputLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/second_input_layout"
android:text="@string/test_input_error"
android:onClick="clickMethod"/>
</RelativeLayout>
package com.example.tyler.myapplication;
import android.os.Bundle;
import android.support.design.widget.TextInputEditText;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
public void clickMethod(View view) {
TextInputLayout firstInputLayout = (TextInputLayout) findViewById(R.id.first_input_layout);
TextInputLayout secondInputLayout = (TextInputLayout) findViewById(R.id.second_input_layout);
// First, clear errors
firstInputLayout.setError(null);
secondInputLayout.setError(null);
// Set errors, if they exist
if (TextUtils.isEmpty(((TextInputEditText) findViewById(R.id.first_edit_text)).getText().toString())) {
firstInputLayout.setError("This field is required");
}
if (TextUtils.isEmpty(((TextInputEditText) findViewById(R.id.second_edit_text)).getText().toString())) {
secondInputLayout.setError("This field is required");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment