6

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"/>
MleChef
  • 321
  • 1
  • 8

3 Answers3

13

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.

MleChef
  • 321
  • 1
  • 8
2

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=" "

Gandora
  • 195
  • 1
  • 18
0

For starters, remove one item at a time from the XML.See if the cursor is being affected by anything else first (such as android:gravity="start" or android:textAppearance="@style/TextAppearance.AppCompat.Medium").

Then post your @drawable/custom_edit_text_background so we can see the XML. Finally, post your class CustomEditText. You may have inadvertently caused an issue in there.


These may help:

Community
  • 1
  • 1
Petro
  • 3,484
  • 3
  • 32
  • 59
  • As stated in the question, I already tried the solutions you suggested. They did not work for me. Those XML attributes have no effect on the issue. – MleChef May 18 '16 at 21:46