I need an EditText
that looks like this onError:
calling onError looks like this instead :
Note: the app is running on SDK 19 (4.4.2)
min SDK is 1
Is there a method similar to setError that does this automatically, or do I have to write the code for it ?
Thank you
There's no need to use a third-party library since Google introduced the
TextInputLayout
as part of thedesign-support-library
.Following a basic example:
Layout
Note: By setting
app:errorEnabled="true"
as an attribute of theTextInputLayout
it won't change it's size once an error is displayed - so it basically blocks the space.Code
In order to show the Error below the
EditText
you simply need to call#setError
on theTextInputLayout
(NOT on the childEditText
):Result
To hide the error and reset the tint simply call
til.setError(null)
.Note
In order to use the
TextInputLayout
you have to add the following to yourbuild.gradle
dependencies:Setting a custom color
By default the line of the
EditText
will be red. If you need to display a different color you can use the following code as soon as you callsetError
.To clear it simply call the
clearColorFilter
function, like this:reVerse's answer is great but it didn't point out how to remove the floating error tooltip kind of thing
You'll need
edittext.setError(null)
to remove that.Also, as someone pointed out, you don't need
TextInputLayout.setErrorEnabled(true)
Layout
Code
Call
myTextInputLayout.setError()
instead ofmyEditText.setError()
.These container and containment have double functionality on setting errors. Functionality you need is container's one. But you could require minimal version of 23 for that.
Your
EditText
should be wrapped in aTextInputLayout
To get an error message like you wanted, set error to
TextInputLayout
You should add design support library dependency. Add this line in your gradle dependencies