209

Does anyone know how to disable the blinking cursor in an EditText view?

Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96
Kevin Parker
  • 16,975
  • 20
  • 76
  • 105

11 Answers11

394

You can use either the xml attribute android:cursorVisible="false" or programatically:

  • java: view.setCursorVisible(false)
  • kotlin: view.isCursorVisible = false
Luís Gonçalves
  • 338
  • 5
  • 17
Vic
  • 5,977
  • 2
  • 22
  • 19
43

Perfect Solution that goes further to the goal

Goal: Disable the blinking curser when EditText is not in focus, and enable the blinking curser when EditText is in focus. Below also opens keyboard when EditText is clicked, and hides it when you press done in the keyboard.

1) Set in your xml under your EditText:

android:cursorVisible="false"

2) Set onClickListener:

iEditText.setOnClickListener(editTextClickListener);

OnClickListener editTextClickListener = new OnClickListener() 

{

    public void onClick(View v) 
    {
         if (v.getId() == iEditText.getId()) 
        {
            iEditText.setCursorVisible(true);
        }

    }
};

3) then onCreate, capture the event when done is pressed using OnEditorActionListener to your EditText, and then setCursorVisible(false).

//onCreate...

iEditText.setOnEditorActionListener(new OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId,
                    KeyEvent event) {
                    iEditText.setCursorVisible(false);
                if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    in.hideSoftInputFromWindow(iEditText.getApplicationWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
                }
                return false;
            }
        });
Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96
coolcool1994
  • 3,704
  • 4
  • 39
  • 43
  • 6
    KeyEvent.KEYCODE_ENTER will not work for all keyboards. You'll need to specifically mention android:imeOptions="actionDone" in the layout and then handle EditorInfo.IME_ACTION_DONE also. – PsyGik Dec 01 '14 at 08:31
  • Great answer just should replace on click with on touch because some time edit text gain focus without on click so in this case the cursor wont be shown – Antwan Feb 18 '16 at 11:26
15

You can use following code for enabling and disabling edit text cursor by programmatically.

To Enable cursor

    editText.requestFocus();
    editText.setCursorVisible(true);

To Disable cursor

    editText.setCursorVisible(false);

Using XML enable disable cursor

   android:cursorVisible="false/true"
   android:focusable="false/true"

To make edit_text Selectable to (copy/cut/paste/select/select all)

   editText.setTextIsSelectable(true);

To focus on touch mode write following lines in XML

  android:focusableInTouchMode="true"
  android:clickable="true"
  android:focusable="true" 

programmatically

  editText.requestFocusFromTouch();

To clear focus on touch mode

  editText.clearFocus()
Sagar
  • 5,273
  • 4
  • 37
  • 50
6

simple add this line into your parent layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:focusable="true"
    android:focusableInTouchMode="true">

    <EditText
        android:inputType="text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>
5

The problem with setting cursor visibility to true and false may be a problem since it removes the cursor until you again set it again and at the same time field is editable which is not good user experience.

so instead of using

setCursorVisible(false)

just do it like this

        editText2.setFocusableInTouchMode(false)
        editText2.clearFocus()
        editText2.setFocusableInTouchMode(true)

The above code removes the focus which in turn removes the cursor. And enables it again so that you can again touch it and able to edit it. Just like normal user experience.

vikas kumar
  • 10,447
  • 2
  • 46
  • 52
4

In my case, I wanted to enable/disable the cursor when the edit is focused.

In your Activity:

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
        View v = getCurrentFocus();
        if (v instanceof EditText) {
            EditText edit = ((EditText) v);
            Rect outR = new Rect();
            edit.getGlobalVisibleRect(outR);
            Boolean isKeyboardOpen = !outR.contains((int)ev.getRawX(), (int)ev.getRawY());
            System.out.print("Is Keyboard? " + isKeyboardOpen);
            if (isKeyboardOpen) {
                System.out.print("Entro al IF");
                edit.clearFocus();
                InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(edit.getWindowToken(), 0);
            }

            edit.setCursorVisible(!isKeyboardOpen);

        }
    }
    return super.dispatchTouchEvent(ev);
}
MontiRabbit
  • 1,020
  • 12
  • 17
  • Pretty sure you want `edit.setCursorVisible(!isKeyboardOpen);` to be `edit.setCursorVisible(isKeyboardOpen);` so that the cursor is visible when the keyboard is present. – Daniel Storm Apr 10 '17 at 18:19
3

If you want to ignore the Edittext from the starting of activity, android:focusable and android:focusableInTouchMode will help you.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout7" android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:focusable="true" android:focusableInTouchMode="true">

This LinearLayout with your Edittext.

halfer
  • 19,824
  • 17
  • 99
  • 186
Sajid khan
  • 595
  • 5
  • 12
3

add android:focusableInTouchMode="true" in root layout, when edit text will be clicked at that time cursor will be shown.

2
  1. Change focus to another view (ex: Any textview or Linearlayout in the XML) using

    android:focusableInTouchMode="true"
    android:focusable="true"
    
  2. set addTextChangedListener to edittext in Activity.

  3. and then on aftertextchanged of Edittext put edittext.clearFocus();

This will enable the cursor when keyboard is open and disable when keyboard is closed.

Daniele Santi
  • 771
  • 3
  • 24
  • 31
Bhavani PK
  • 61
  • 3
1

In kotlin your_edittext.isCursorVisible = false

0

rootLayout.findFocus().clearFocus();

Qasimodo
  • 21
  • 5