Change Android EditText Cursor Height


To Change the default height of EditText Cursor we need to create a drawable XML file with shape tag.

Create editext_cursor.xml and place it under res->drawable directory.

  1. Set the shape as rectangle.
  2. Using size tag we can set the width of the cursor.
  3. Using solid tag we can set the color of EditText Curosor.

To adjust the height of the cursor we need to make use of padding tag, with attributes android:bottom and android:top we can trim or increase the cursor height. This could be really useful when you are creating a custom EditText with ruled lines (like a notebook).

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <size android:width="1dip" />
    <solid android:color="#010101" />
    <padding
        android:bottom="-11sp"
        android:top="-2sp" />
    <stroke android:color="#e71839" />
</shape>

Now in your layout file add android:textCursorDrawable="@drawable/editext_cursor" to the EditText tag in the activity.xml file.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ebm="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f2f2f2"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">


    <EditText

        android:id="@+id/view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scrollbars="vertical"
        android:textCursorDrawable="@drawable/editext_cursor"
        android:textSize="20sp">

    </EditText>


</RelativeLayout>

Note that this will only work if you have set mimSDK as 12 or above.



















Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap