Kotlin Change Cursor Color EditText (KCCCE)

To change the cursor color on an EditText in Android, you can use the android:textCursorDrawable attribute in XML or the setCursorDrawableResource() method in Java or Kotlin code. Here's an example of how to do it:

1. Tricky Way

Set this attribute to @null 

android:textCursorDrawable="@null"

Then you will see the cursor color is the same as the text color.

2. Using XML (Layout)

Add the android:textCursorDrawable attribute to the EditText element in your XML layout file. For example:

<EditText 
    android:id= "@+id/editText" 
    android:layout_width= "match_parent" 
    android:layout_height= "wrap_content" 
    android:textCursorDrawable= "@color/colorAccent"  />

Make sure to create the colors you want to use in the res/values/colors.xml folder:
<resources> 
    <color  name= "colorAccent" > #FF4081 </color>  <!-- Replace with desired color --> 
</resources>

3. Programmatically

val editText: EditText = findViewById(R.id.editText)
editText.setCursorDrawableResource(R.color.colorAccent) // Replace with desired color

Post a Comment

Previous Next

نموذج الاتصال