I have not found any solution to edit error style, but I have created custom EditText with error popup. Hopefully, it will help:
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? = null
fun showError(message: String) {
showErrorPopup(message)
}
fun showError(@StringRes messageResId: Int) {
showErrorPopup(context.getString(messageResId))
}
fun dismissError() {
post {errorPopup?.dismiss() }
}
private fun showErrorPopup(message: String) {
post {
dismissError()
val inflater = (context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater)
val view = inflater.inflate(R.layout.edit_text_error, null, false)
errorPopup =
PopupWindow(view, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
errorPopup?.contentView?.findViewById<TextView>(R.id.message)?.text = message
errorPopup?.showAsDropDown(this, 0, 10, Gravity.START)
}
}
}
You can use methods:
showError(message: String)
or showError(@StringRes messageResId: String)
to show the error and method dismissError()
to hide it.
Keep in mind that pop-ups are bound with activity lifecycle if you are using multiple fragments to navigate through the app, remember about closing them when navigating.
Here is my edit_text_error layout used for popup:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:elevation="2dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:background="#fff">
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#E20000"
android:padding="8dp"
android:textSize="11sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:text="Please enter a valid email address!" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
If you'd like to move popup to the end of EditText, please change last line in showErrorPopup()
method to:
errorPopup?.showAsDropDown(this, 0, 10, Gravity.END)
You can manipulate the position of the popup by modifying gravity or x and y parameters of the showAsDropDown(view: View, x: Int, y: Int, gravity: Gravity)
method