@@ -12,7 +12,6 @@ import android.view.MotionEvent
1212import android.view.View
1313import android.view.ViewGroup
1414import android.view.inputmethod.EditorInfo
15- import androidx.annotation.ColorInt
1615import androidx.annotation.Dimension
1716import androidx.annotation.Px
1817import androidx.annotation.RequiresApi
@@ -71,7 +70,8 @@ public open class LabTextField @JvmOverloads constructor(
7170 private var textPaddingHorizontal: Int = NO_VALUE_INT
7271
7372 // A temporary solution to update the cursor color until the Material lib's support for this becomes available
74- private var cursorColorOverride: Int = NO_VALUE_INT
73+ private var cursorColorOverride: ColorStateList ? = null
74+ private var cursorErrorColorOverride: ColorStateList ? = null
7575
7676 init {
7777 attrs?.let {
@@ -90,6 +90,11 @@ public open class LabTextField @JvmOverloads constructor(
9090 }
9191
9292 boxHelper = LabTextFieldBoxBackgroundHelper (this , attrs, defStyleAttr)
93+
94+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .Q ) {
95+ cursorColorOverride = cursorColor
96+ cursorErrorColorOverride = cursorErrorColor
97+ }
9398 }
9499
95100 override fun addView (child : View , index : Int , params : ViewGroup .LayoutParams ) {
@@ -256,11 +261,31 @@ public open class LabTextField @JvmOverloads constructor(
256261 * Set a color for the cursor and text select handles. This overrides the default color from colorControlActivated value.
257262 * NB! Only supported on API 29+
258263 *
259- * @param color The new color override
264+ * We want to modify the default behaviour here because the material lib's cursorColor does not override the select handle colors.
265+ *
266+ * @param cursorColor The new color override
267+ * TODO: material 1.12.0: Check if select handles are added
260268 */
261269 @RequiresApi(Build .VERSION_CODES .Q )
262- public fun setCursorColorOverride (@ColorInt color : Int ) {
263- cursorColorOverride = color
270+ override fun setCursorColor (cursorColor : ColorStateList ? ) {
271+ super .setCursorColor(cursorColor)
272+ cursorColorOverride = cursorColor
273+ updateCursorColor()
274+ }
275+
276+ /* *
277+ * Set a color for the cursor and text select handles in error mode. This overrides the default color from colorControlActivated value.
278+ * NB! Only supported on API 29+
279+ *
280+ * We want to modify the default behaviour here because the material lib's cursorErrorColor does not override the select handle colors.
281+ *
282+ * @param cursorErrorColor The new color override
283+ * TODO: material 1.12.0: Check if select handles are added
284+ */
285+ @RequiresApi(Build .VERSION_CODES .Q )
286+ override fun setCursorErrorColor (cursorErrorColor : ColorStateList ? ) {
287+ super .setCursorErrorColor(cursorErrorColor)
288+ cursorErrorColorOverride = cursorErrorColor
264289 updateCursorColor()
265290 }
266291
@@ -390,7 +415,7 @@ public open class LabTextField @JvmOverloads constructor(
390415 }
391416
392417 private fun updateCursorColor () {
393- if (Build .VERSION .SDK_INT < Build .VERSION_CODES .Q || cursorColorOverride == NO_VALUE_INT ) {
418+ if (Build .VERSION .SDK_INT < Build .VERSION_CODES .Q ) {
394419 // Not supported.
395420 return
396421 }
@@ -401,10 +426,19 @@ public open class LabTextField @JvmOverloads constructor(
401426 return
402427 }
403428
404- DrawableUtil .setDrawableColor(editText.textSelectHandleLeft, cursorColorOverride)
405- DrawableUtil .setDrawableColor(editText.textSelectHandleRight, cursorColorOverride)
406- DrawableUtil .setDrawableColor(editText.textSelectHandle, cursorColorOverride)
407- DrawableUtil .setDrawableColor(editText.textCursorDrawable, cursorColorOverride)
429+ val color = if (isOnError()) cursorErrorColorOverride else cursorColorOverride
430+ if (color == null ) {
431+ return
432+ }
433+
434+ DrawableUtil .setTintList(editText.textSelectHandleLeft, color)
435+ DrawableUtil .setTintList(editText.textSelectHandleRight, color)
436+ DrawableUtil .setTintList(editText.textSelectHandle, color)
437+ DrawableUtil .setTintList(editText.textCursorDrawable, color)
438+ }
439+
440+ private fun isOnError (): Boolean {
441+ return errorState
408442 }
409443
410444 internal companion object {
0 commit comments