Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write style to error text of EditText in android?

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

enter image description here

like image 580
ibrahimyilmaz Avatar asked Jan 19 '13 10:01

ibrahimyilmaz


People also ask

How do I show errors in EditText?

Solution: We can display error message in EditText by using setError() method.

How do I show error in TextInputLayout?

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.

How do I change the style of edit text in Android Studio?

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.

What is TextInputLayout?

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.


2 Answers

The solution is at the end and here is the screenshot:

Styled error


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

like image 52
Sherif elKhatib Avatar answered Sep 19 '22 03:09

Sherif elKhatib


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 :-(

like image 36
Dheeraj Vepakomma Avatar answered Sep 20 '22 03:09

Dheeraj Vepakomma