The custom TextInputLayout results in the below exception when trying to achieve errorState.
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.content.res.ColorStateList.isStateful()' on a null object reference at com.google.android.material.textfield.TextInputLayout.setBoxStrokeColorStateList(TextInputLayout.java:1103) at tech.********.platform.components.MyCustomTextInput.setErrorEnabled(MyCustomTextInput.kt:81) at com.google.android.material.textfield.TextInputLayout.<init>(TextInputLayout.java:786) at com.google.android.material.textfield.TextInputLayout.<init>(TextInputLayout.java:422) at tech.********.platform.components.MyCustomTextInput.<init>(MyCustomTextInput.kt:38) XML code to switch app:errorEnabled with binding
<MyCustomTextInput> android:id="@+id/inputPassword" ... app:errorEnabled="@{condition ? true: false}" <MyCustomTextInput/> trying to achieve error stoke colour without error text as below:
override fun setErrorEnabled(enabled: Boolean) { if (enabled) setBoxStrokeColorStateList(errorColor) else setBoxStrokeColorStateList(defaultColor) super.setErrorEnabled(false) } complete Layout code below:
class MyCustomTextInput : TextInputLayout { constructor(context: Context) : super(context) constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super( context, attrs, defStyleAttr ) /** * Color states for the default scenario. */ private var defaultColor = ColorStateList( arrayOf( intArrayOf(-R.attr.state_focused, R.attr.state_enabled), intArrayOf(R.attr.state_focused, R.attr.state_enabled), ), intArrayOf(Color.parseColor("#CCCCCC"), Color.parseColor("#164A9C")) ) /** * Color states for the error scenario. */ private var errorColor = ColorStateList( arrayOf( intArrayOf(-R.attr.state_focused, R.attr.state_enabled), intArrayOf(R.attr.state_focused, R.attr.state_enabled) ), intArrayOf(Color.RED, Color.RED) ) init { setBoxStrokeColorStateList(defaultColor) // edit text stroke hintTextColor = defaultColor // edit text content } override fun setErrorIconDrawable(errorIconDrawable: Drawable?) { super.setErrorIconDrawable(null) } override fun setErrorIconTintList(errorIconTintList: ColorStateList?) { super.setErrorIconTintList(errorColor) } override fun setErrorEnabled(enabled: Boolean) { if (enabled) setBoxStrokeColorStateList(errorColor) else setBoxStrokeColorStateList(defaultColor) super.setErrorEnabled(false) } override fun onDraw(canvas: Canvas) { super.onDraw(canvas) } } Using the same colorState as used in the init().