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

up vote 305 down vote accepted

You can use either the xml attribute android:cursorVisible="false" or the java function setCursorVisible(false).

  • 10
    Awesome, this together with onclicklistener that makes it visible gives the correct behaviour :) – Warpzit Apr 27 '12 at 8:59
  • android:cursorVisible="false" (worked for me. thanks) – Fawwad Shafi May 15 '13 at 9:45
  • @cerin is there a way to hide the blue marker so I can disable the paste, but keep the cursor visible so the user will see where is he in the field? – limlim Aug 21 '14 at 14:05
  • It works for me. Thanks! – Bhavik Kamdar Jan 7 '16 at 7:16
  • 5
    Is it possible to disable just the blinking itself, and not the cursor? – android developer Feb 1 '16 at 11:40

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;
            }
        });
  • 5
    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 1 '14 at 8: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

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()

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);
}
  • 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

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.