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

Development issue/problem:

I am currently trying to write a new custom style for my Android application. I need to style the error text that appears after setting SetError in EditText.

How do I set the style?

For example: I want to put the background in white and the color: Blue, etc. in style.xml

How can I solve this problem?

Solution 1:

The solution is at the end, and here’s a screenshot:

A few clarifications

You can define the text color with the following line

yourEditText.setError(Html.fromHtml(this is an error)) ;

However, this cannot be guaranteed.

According to the source code, this popup window that appears has the type ErrorPopup, which is an internal class within TextView. The content of this popup is a simple TextView created by com.android.internal.R.layout.textview_hint.

TextView err = (TextView) inflater.inflate(com.android.internal.R.layout.textview_hint,
null) ;

The background of this window depends on whether it should be placed above the anchor:

if (above) {
mView.setBackgroundResource(com.android.internal.R.drawable.popup_inline_error_above);
} otherwise {
mView.setBackgroundResource(com.android.internal.R.drawable.popup_inline_error);
}

Since all Android resources used to create pop-ups are internal and ultimately hard-coded, the best option would be to make your own pop-up with errors. It would be very easy, and you wouldn’t interfere with the normal EditText, as the default popup is only used to display an error, so it would be nice to create your own popup.

DECISION

I made it here: Widgets

Solution 2:

I don’t think you can change the style this way, because the error popup uses an internal style:

mPopupInlineErrorBackgroundId = getResourceId(mPopupInlineErrorBackgroundId,
com.android.internal.R.styleable.Theme_errorMessageBackground) ;
mView.setBackgroundResource(mPopupInlineErrorBackgroundId) ;

However, you can set the stepped symbol and the adjusted error with the overloaded function setError (CharSequence, Drawable).

With the fromHtml() function you can easily create overwrite HTML.

However, you still cannot set a pop-up background.

Solution 3:

Add it when validating the form if the input field is empty.

int ecolor = R.color.black; // any color
String estring = Please enter a correct email address;
ForegroundColorSpan fgcspan = new ForegroundColorSpan(ecolor);
SpanableStringBuilder ssbuilder = new SpanableStringBuilder(estring);
ssbuilder.setSpan(fgcspan, 0, estring.length(), 0) ;

edtEmail.requestFocus() ;
edtEmail.setError(ssbuilder) ;

When you write to an editable text, an error mark is automatically activated.

Thanks
Sachin

Solution 4:

I saw the answer accepted, but I don’t like the proposed library.

I think it’s a bug in Android, and I submitted a bug here:
https://code.google.com/p/android/issues/detail?id=158590.

EDIT:
The TextInputLayout widget of the Android design library can be used to get better error handling in EditText.

Let’s see what it looks like here:

And how to implement it:
http://code.tutsplus.com/tutorials/creating-a-login-screen-using-textinputlayout-cms-24168-cms-24168

Solution No 5:

Follow this link to get a nice error message in the hardware design look!
Hardware doc

I see that the link is broken, so here’s how to do it:
use TextInputLayout

and in the TextWatcher code (I use the data link), call setError on your TextInputLayout

binding.ipAddress.setError(your error) ;

That’s how I did it:

binding.ipAddress.getEditText().addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Check public blank
naTextChanged (Editable) {

if(! Patterns.IP_ADDRESS.matcher(editable.toString()).matches()) {
binding.ipAddress.setError(your string error);
IpAddressOK = false;
}
different{
binding.ipAddress.setError(null);
IpAddressOK = true;
}
}
}))

Solution No 6:

It works very well!

private fun setErrorOnSearchView(searchView : SearchView, errorMessage :
String) {
trap id = searchView.context
.resources
.getIdentifier(android:id/search_src_text, null)
trap editText = searchView.find(id)

fall errorColor = ContextCompat.getColor (this,R.color.red)
fall fgcspan = ForegroundColorSpan(errorColor)
fall builder = SpannableStringBuilder(errorMessage)
builder.setSpan(fgcspan, 0, errorMessage.length, 0)
editText.error = builder
}

Solution No 7:

I couldn’t find a solution to change the error style, but I created a custom EditText with an error popup. I hope this helps you:

import android.content.Context
import android.util.AttributeSet
import android.view.Gravity
import android.view.LayoutInflater
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.PopupWindow
import android.widget.TextView
import androidx.annotation.stringRes.

class ErrorEditText : EditText

constructor(context: Context): super(context)

constructor(context: Context, attrs: AttributeSet): super(context, attrs)

constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int): super(context, attrs, defStyleAttr)

private var errorPopup : PopupWindow? = Invalid

fun showError(message: String) {
showErrorPopup(message)
}

fun showError(@StringRes messageResId: Int) {
showErrorPopup(context.getString(messageResId))
}

pleasant dismissalError() {
message {errorPopup?.dismiss() }
}

private fun showErrorPopup(message: String) {post {dismissError()fall inflater = (context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater)fall view = inflater.inflate(R.layout.edit_text_error, null, false)errorPopup =PopupWindow(view, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)errorPopup ?.contentView ?.findViewById(R.id.message) ?.text = redirectorrorPopup ?.showAsDropDown(this, 0, 10, Gravity.START)}}}}.

To display the error, methods can be used:
showError(message: String) or showError(@StringRes messageResId: String), and to hide it, the dismissError() method.

Remember that pop-ups are associated with a life cycle of activity. If you are using multiple fragments to navigate through the application, remember to close them while you are navigating.

Here is the layout of my edit_text_error used for popups :

If you want to move the popup to the end of EditText, change the last line of the showErrorPopup() method to :
errorPopup ?.showAsDropDown(this, 0, 10, Gravity.END)

You can manipulate the position of the pop-up window by changing the parameters x and y of the showAsDropDown method (view: View, x: Int, y: Int, Gravity: Gravity).

Good luck!

Related Tags:

android-edittext error background,android edittext red border on error,seterror color,android edittext box style,textinputlayout error background color,how to show error message below edittext in android,custom edittext android,working with edittext in android,different types of edittext in android,edittext show error message,android-edittext seterror,material edittext error android,android edittext style,outlined text fields android

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *