I'm trying to write new custom style for my android application. I need to give style to errorText which appears after setting setError
in EditText
.
How can I customize style of it?
For example: I want to set its background
white and textColor
: Blue etc etc. in style.xml
Solution: We can display error message in EditText by using setError() method.
You can use the TextInputLayout to display error messages according to the material design guidelines using the setError and setErrorEnabled methods. In order to show the error below the EditText use: TextInputLayout til = (TextInputLayout) findViewById(R. id.
You can use the attribute style="@style/your_style" that is defined for any widget. The attribute parent="@android:style/Widget. EditText" is important because it will ensure that the style being defined extends the basic Android EditText style, thus only properties different from the default style need to be defined.
TextInputLayout is a view container that is used to add more features to an EditText. It acts as a wrapper for EditText and has some features like: Floating hint. Animation that can be disabled or enabled. Error labels that display error messages when an error occurs.
The solution is at the end and here is the screenshot:
Some Explanation
You might be able to set the textcolor using the following line
yourEditText.setError(Html.fromHtml("<font color='blue'>this is the error</font>"));
However, this might not be guaranteed.
According to the source code, this Popup
that shows is of type ErrorPopup
which is an internal class inside TextView
. The content of this Popup
is a single TextView
inflated from com.android.internal.R.layout.textview_hint
final TextView err = (TextView) inflater.inflate(com.android.internal.R.layout.textview_hint, null);
The background of this Popup
depends on whether it should be placed above the anchor:
if (above) { mView.setBackgroundResource(com.android.internal.R.drawable.popup_inline_error_above); } else { mView.setBackgroundResource(com.android.internal.R.drawable.popup_inline_error); }
Since all the android resources used to create the popup are internal and ultimately hard-coded, your best shot would be to create your own error popup. This would be very easy and you wouldn't really be interfering with the normal EditText
because the default popup is merely used to show the error, and, thus, creating your own would be fine.
SOLUTION
I have created it here: WidgyWidgets
I don't think you can customize its style that way since the error popup uses an internal style:
mPopupInlineErrorBackgroundId = getResourceId(mPopupInlineErrorBackgroundId, com.android.internal.R.styleable.Theme_errorMessageBackground); mView.setBackgroundResource(mPopupInlineErrorBackgroundId);
However, you can set a Spanned
and a custom error icon using the overloaded setError(CharSequence, Drawable)
.
You can easily create a Spanned
from HTML using fromHtml()
.
However, you still won't be able to set the popup background image :-(
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With