21

I have an EditText input in Android 4.0 and the Cursor is not showing inside it.

What can make the cursor not appear in the input field?

Deˣ
  • 4,191
  • 15
  • 24
Akmal Rasool
  • 496
  • 2
  • 7
  • 17

12 Answers12

60

Make android:cursorVisible="true"

and

If you have used android:textColor then set the android:textCursorDrawable attribute to @null.

Happy coding ;)

Abraham Philip
  • 648
  • 9
  • 18
Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
  • 1
    android:textCursorDrawable is for API levels 12 and above. Works like a charm though. Any solution for lower versions? – Prachi Nov 17 '14 at 12:24
  • 1
    android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:textColor="@android:color/black" android:background="@drawable/round_rect_shape" android:cursorVisible="true" android:textCursorDrawable="@null" android:hint="Amount"> – stephen ebichondo Mar 13 '15 at 18:04
  • 1
    I've upvoted this answer because it solved my issue (textCursorDrawable), but your reason for it is slightly erroneous. textCursorDrawable="@null" will make your cursor color the same as your textColor, but you don't need to use android:textColor for it to work. textCursorDrawable solves the cursor issue if the reason the cursor can't be seen is because it's the same color as the EditText background (generally white). – Abraham Philip Jun 04 '15 at 23:52
  • You are awesome, that was the issue I was stuck in android:textCursorDrawable="@null" – Naveed Ahmad Jun 09 '15 at 15:02
9

I had a similar problem but it was because the cursor is actually white and I had a white background. I needed to be able to change the cursor to black in code somehow and used this approach.

I created a layout resource called textbox.axml which contained this

   <?xml version="1.0" encoding="utf-8"?>
   <EditText xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="This is a template"
    android:background="#ffffff"
    android:textColor="#000000"
    android:cursorVisible="true"
    android:textCursorDrawable="@null" />

I then applied this layout in code (C# because I am using Xamarin) thus

    EditText txtCompletionDate = (EditText)LayoutInflater.Inflate(Resource.Layout.textbox, null);

but it is similar in Java.

Ben Messenegr
  • 156
  • 1
  • 1
6

Add this line for your edit text in the xml file.

android:textCursorDrawable="@null"
Emran Hamza
  • 3,829
  • 1
  • 24
  • 20
4

I happened quite same problem - cursor was showing up only after user types some characters. I tried solutions listed here, but without any success on my device. What actually work for me, is setting blank text to my edittext:

EditText editText = findViewById(R.id.edit_text);
editText.setText("");

This "fakes" the user input, and cursor appears.

Anhayt Ananun
  • 896
  • 1
  • 11
  • 27
  • thks, this is the only answer solved my problem, it takes me hours. i think it's mightbe a bug of edittext – oscarthecat Jun 18 '15 at 08:01
  • 1
    Actually it didn't work for me, but `editText.setText(" ")` (with a _space_) did – marco Jul 13 '15 at 13:20
  • I set the hint instead to a whitespace. I know it's not an option for everyone but at least you won't have to set the text to a whitespace. – Gandora Apr 20 '21 at 07:12
4

My issue was that I was using the AppCompat theme, but I had some custom view classes that extended EditText that needed to extend AppCompatEditText in order for the AppCompat style to be applied correctly.

Darren Hinderer
  • 418
  • 1
  • 6
  • 12
  • 1
    To me, this seems to be the correct answer. Setting the cursorDrawable to @null also changes the cursor's appearance, it looks very thin in my case. Changing from EditText to AppCompatEditText fixes the problem *and* displays the cursor correctly. – Benjamin May 03 '16 at 04:43
1

Add this line for your edit text in the xml file.

android:cursorVisible="true"
King of Masses
  • 18,405
  • 4
  • 60
  • 77
Droidee
  • 94
  • 5
1

Just adding my own personal fix to anyone it might help. I had tried everything here but forcing android:background="@null" was causing a very tiny cursor only at the end of my right aligned TextEdit (it was right working fine elsewhere).

Simply adding android:padding="1dp" in my TextEdit solved the issue.

apouche
  • 9,703
  • 6
  • 40
  • 45
1

As mentioned above, here's the actual line
android:textCursorDrawable="@null"

 <EditText
               android:textCursorDrawable="@null"
                android:imeOptions="actionNext"
                android:id="@+id/edSMobile"
                 android:layout_width="match_parent"
                android:layout_height="wrap_content"                    
                android:background="@drawable/edit_corner"                    
                android:inputType="phone" />
Mani
  • 930
  • 10
  • 14
0

I found what was causing it to happen to me.

You need to inherit it from the application's theme. I'm not sure what the line item needs to be exactly, but android:Theme has it so inheriting that will do that trick.

Using the default AppBaseTheme will work (it has android:Theme.Light as it's parent).

To use AppBaseTheme put android:theme="@style/AppBaseTheme" into your application tag in the manifest. You can also use a custom style and multiple levels of inheritance so long as one of them has parent="android:Theme" in the style tag.Like I said it may be possible to have it without that, just using certain line item(s) but I don't know what those would be.

If you don't need a custom theme you can just use

android:theme="@android:style/Theme"

user2312638
  • 443
  • 2
  • 6
  • 17
0

In My case the cursor is visible if user language is English but if he change his language to Arabic then its not visible.

To fix this I have created on custom drawable for cursor.

Cursur shap at drawable/black_cursor.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <solid android:color="#a8a8a8"/><!-- This is the exact color of android edit text Hint -->
    <size android:width="1dp" />
</shape>

Edit Text:

<EditText
    android:id="@+id/user_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:textCursorDrawable="@drawable/black_cursor"
    />
Gem
  • 1,516
  • 16
  • 21
0

If the EditText has a Background drawable with a border, the cursor is displaying in the border and appears to be invisible. To rectify the problem set padding in the EditText to a small amount e.g. 5dp

0

If you want to show the cursor then just do

android:textCursorDrawable="@null".
tej shah
  • 2,995
  • 2
  • 25
  • 35