Oleksii Yerastov
11/03/2020, 11:17 AMEditText
.
For API < Android Q I’m using reflection and it works like intended. Starting from Android Q we have convenient method setTextCursorDrawable(@Nullable Drawable textCursorDrawable)
. I use it like following:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
textCursorDrawable = ContextCompat.getDrawable(context,R.drawable.drawable_cursor)!!.tintDrawable(color)
}
Where tintDrawable
method is:
private fun Drawable.tintDrawable(@ColorInt color: Int): Drawable {
return when (this) {
is VectorDrawableCompat -> this.apply { setTintList(ColorStateList.valueOf(color)) }
is VectorDrawable -> this.apply { setTintList(ColorStateList.valueOf(color)) }
else -> {
val wrappedDrawable = DrawableCompat.wrap(this)
DrawableCompat.setTint(wrappedDrawable, color)
DrawableCompat.unwrap(wrappedDrawable)
}
}
}
and R.drawable.drawable_cursor
is just simple rectangle shape:
<shape xmlns:android="<http://schemas.android.com/apk/res/android>" >
<size android:width="2dp" />
<solid android:color="?android:textColorPrimary" />
</shape>
BUT there is no visible effect at all. Even without applying tint cursor drawable just stays the same.
Maybe someone faced this problem and have a solution for it or can say what I am doing wrong?Casey Brooks
11/04/2020, 3:23 PMOleksii Yerastov
11/04/2020, 3:27 PM