0

Looking for a way to style the error message for material.textfield.TextInputLayout

such that the error message has a background with the following characteristics

  • filled in color
  • customizable background shape
  • shadows

similar to this error message

enter image description here

and I was considering using a nine patch

https://developer.android.com/studio/write/draw9patch

but I'm not sure how to define the background for the error message as described above

the_prole
  • 8,275
  • 16
  • 78
  • 163

1 Answers1

0
import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.widget.FrameLayout
import android.widget.TextView
import com.google.android.material.textfield.TextInputLayout

class TextInputLayoutCustomError : TextInputLayout {

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

    override fun setErrorEnabled(enabled: Boolean) {
        super.setErrorEnabled(enabled)
        if (enabled) {
            enableError()
        }
    }

    private fun enableError() {
        val errorView = findViewById<TextView>(com.google.android.material.R.id.textinput_error)
        (errorView?.parent as? FrameLayout)?.let { parentFrameLayout ->
            customizeErrorContainer(parentFrameLayout)
        }
    }

    private fun customizeErrorContainer(frameLayout: FrameLayout) {
        frameLayout.setBackgroundColor(Color.RED)
    }
}
the_prole
  • 8,275
  • 16
  • 78
  • 163