I know it is possible to change the cursor appearance by setting a custom drawable to android:textCursorDrawable
but is there any way to get the height of the default cursor drawable?
-
what you want to achieve from the height of the cursor ? – Smit Aug 15 '16 at 15:53
-
@Smit I'm trying to place a transparent view over edittext which has width of edittext but height of cursor. – Siva Aug 15 '16 at 15:57
-
@Smit Thanks for the link. It does say about changing the height of cursor. But I'm looking to get the default height of cursor set by android. – Siva Aug 15 '16 at 16:08
4 Answers
This is untested. But this should work.
public int getHeightofCursor(TextView textView) {
Paint.FontMetricsInt fm = textView.getPaint().getFontMetricsInt();
int cursorHeight = fm.bottom - fm.top;
return cursorHeight;
}

- 1,548
- 22
- 27
Reference your textCursorDrawable in code for example:
Drawable cursor = getResources().getDrawable(android.R.drawable.your_drawable);
And then once you have your drawable use the Drawable Wrapper to wrap you drawable and use the appropriate method to get the height: Like this:
DrawableWrapper with_height_cursor = new DrawableWrapper(cursor);
int height = with_height_cursor.getIntrinsicHeight();
Hope this helps!

- 1,892
- 16
- 31
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.
- Set the shape as rectangle.
- Using size tag we can set the width of the cursor.
- 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.
<?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>

- 2,391
- 1
- 23
- 40
Using reflection i could find the height of Cursor. It has worked for Samsung edge s7. It will probably work for all devices, if the manufacturer did not make any changes to mCursorDrawableRes.
public int getHeightofCursor(Context context, EditText editText) throws Exception {
Field cursor = TextView.class.getDeclaredField("mCursorDrawableRes");
cursor.setAccessible(true);
int resid = cursor.getInt(editText);
Drawable cursorDrawable = context.getResources().getDrawable(resid);
return cursorDrawable.getIntrinsicHeight();
}

- 355
- 1
- 6
- 19
-
Reference from, http://stackoverflow.com/questions/11554078/set-textcursordrawable-programatically – Siva Aug 15 '16 at 16:37
-
Reflective access to mCursorDrawableRes will throw an exception when targeting API 31 and above – anro Aug 31 '22 at 12:13