Android Custom Edit Text Cursor Not Visible

Android Custom Edit Text Cursor Not Visible

Story

I created a new simple project with a custom edit text without any logic (it just extends EditText). The code is the following:

CustomEditText.java

package com.example.customedittextcursor;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.EditText;

/**
 * @author Diana Prața
 *         Date: 5/29/2015.
 */
public class CustomEditText extends EditText {
    public CustomEditText(Context context) {
        super(context);
    }

    public CustomEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
}

activity_main.xml

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

    <com.example.customedittextcursor.CustomEditText
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Address"/>

</RelativeLayout>

 

Problem

The cursor is NOT visible if I use my custom edit text. It seems that its color is set as white (you can change the background and you will see that it is white).

Custom Edit Text - Cursor NOT visible
Custom Edit Text – Cursor NOT visible

But if I use EditText in the xml file, the cursor will be visible.

<EditText
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Address"/>
EditText - Cursor Visible
EditText – Cursor Visible

So it seems that the problem happens only for the custom edit text, even though it does nothing except for extending the EditText class(yes, I know, it’s a very weird behaviour :D, but that’s how it happens)

Solution

In the custom class, we have to extend AppCompatEditText instead of EditText.

package com.example.customedittextcursor;

import android.content.Context;
import android.support.v7.widget.AppCompatEditText;
import android.util.AttributeSet;
import android.widget.EditText;

/**
 * @author Diana Prața
 *         Date: 5/29/2015.
 */
public class CustomEditText extends AppCompatEditText {
    public CustomEditText(Context context) {
        super(context);
    }

    public CustomEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
}

And now, your Custom edit text should have a visible cursor 😀

Note: I did some tests and it seems that only starting Android 4 and above this strange behaviour happens. For Android 2.3.3 for example I could use a custom edit text which extends EditText class and it worked just fine.

Also, I want to mention that other people suggest to add the following attribute to the xml file where you declare the edit text:

android:textCursorDrawable="@null"

It works, but if you use colors for your cursor, the color will not be changed. It will be always gray. So by extending AppCompatEditText you will keep your cursor’s style the same as the other cursors from your app.

android, cursor, Custom EditText, EditText, invisible cursor
Previous Post
Android onActivityResult not called from fragment
Next Post
GSON & Realm String array

Related Posts

keyboard_arrow_up