I want to set custom drawable as cursor for `EditT...
# android
o
I want to set custom drawable as cursor for 
EditText
. 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:
Copy code
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
   textCursorDrawable = ContextCompat.getDrawable(context,R.drawable.drawable_cursor)!!.tintDrawable(color)
}
Where 
tintDrawable
 method is:
Copy code
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:
Copy code
<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?
c
This is not a general-purpose Android Slack channel, it’s specifically for support related to Kotlin as used in an Android app. There are other resources, such as StackOverflow or http://android-united.community/ which will better serve you for general Android help.
o
There is no answers on StackOverflow for a long time so I tried here. Didn’t know about “related to kotlin” part, thank you.
240 Views