There is no need to use a third-party library, as Google introduced TextInputLayout as part of the design-support-library .
Following the basic example:
Markup
<android.support.design.widget.TextInputLayout android:id="@+id/text_input_layout" android:layout_width="match_parent" android:layout_height="wrap_content" app:errorEnabled="true"> <android.support.design.widget.TextInputEditText android:id="@+id/edit_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Enter your name" /> </android.support.design.widget.TextInputLayout>
Note. . By setting app:errorEnabled="true" as the TextInputLayout attribute, it will not change its size after the error is displayed, so it basically blocks the space.
The code
To show the Error below EditText , you just need to call #setError in TextInputLayout:
TextInputLayout til = (TextInputLayout) findViewById(R.id.text_input_layout); til.setError("You need to enter a name");
Result

To hide the error and reset the hue just call til.setError(null) .
Note
To use TextInputLayout, you must add the following to your build.gradle dependencies:
dependencies { compile 'com.android.support:design:25.1.0' }
Custom color customization
By default, the EditText line will be red. If you need to display a different color, you can use the following code as soon as you raise setError .
editText.getBackground().setColorFilter(getResources().getColor(R.color.red_500_primary), PorterDuff.Mode.SRC_ATOP);
To clear it, just call the following line:
editText.getBackground().clearColorFilter;
reVerse Jun 20 '15 at 11:39 2015-06-20 11:39
source share