Monday, September 26, 2016

android - How to change EditText cursor color

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f7fcff"
    android:padding="10dp"
    >
    <EditText
        android:id="@+id/et"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top|center_horizontal"
        android:textSize="25sp"
        android:hint="White some text here."
        android:padding="5dp"
        android:layout_marginTop="50dp"
        android:textCursorDrawable="@drawable/edittext_cursor"
        />
</android.support.design.widget.CoordinatorLayout>
res/drawable/edittext_cursor.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <!-- edit text cursor width 2 dp -->
            <size android:width="2dp"/>
            <!-- edit text cursor color red -->
            <solid android:color="#ff0000"/>
        </shape>
    </item>
</selector>

2. Set EditText cursor color in XML

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:padding="10dp"
    >
    <EditText
        android:id="@+id/et"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top|center_horizontal"
        android:textSize="25sp"
        android:hint="White some text here."
        android:padding="5dp"
        android:layout_marginTop="50dp"
        android:textCursorDrawable="@null"
        android:textColor="#15a305"
        />
</android.support.design.widget.CoordinatorLayout>