Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cursor is not visible in empty custom EditText on KitKat

I have created a custom EditText class that extends android.widget.EditText. When running the app on KitKat and previous OS versions, if the custom EditText is empty, the cursor does not appear. It will appear once text is entered.

I have tried multiple solutions posted on this site including adding android:textCursorDrawable="@null" and android:textIsSelectable="true" to the XML as well as adding those properties programmatically. None of these solutions has worked.

The custom EditText does have a background I am setting, but it needs to be there. Design constraints mean that I must set a non-default background.

So the question is, how do I make the cursor appear when the custom EditText is empty?

Here is the XML for the custom view:

<?xml version="1.0" encoding="utf-8"?>
<com.example.CustomEditText
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:background="@drawable/custom_edit_text_background"
    android:gravity="start"
    android:imeOptions="actionDone"
    android:inputType="textCapSentences|textMultiLine"
    android:maxLines="5"
    android:padding="@dimen/custom_padding"
    android:textAppearance="@style/TextAppearance.AppCompat.Medium"
    android:textColor="@color/custom_color"/>
like image 376
MleChef Avatar asked May 18 '16 20:05

MleChef


2 Answers

The solution I found is to set a minWidth on the custom EditText. A value of 40dp worked for me. Now the cursor will appear in the view, even when empty.

like image 104
MleChef Avatar answered Nov 01 '22 21:11

MleChef


Just adding the solution that worked for me: I set the hint property on EditText. Cannot be empty though, so I used a whitespace.

In my case android:hint=" "

like image 1
Gandora Avatar answered Nov 01 '22 21:11

Gandora