30

I have a single EditText on my layout. After the user inputs some text and hits the "done" key, I would like to remove the blinking cursor from it. I have scoured StackOverflow and found 3 answers that didn't work for me. The blinking cursor still remains.

private class MyOnKeyListener implements OnKeyListener {
  public boolean onKey(View v, int keyCode, KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN
    && keyCode == KeyEvent.KEYCODE_ENTER) {
      // FAIL 0
      MyActivity.this.findViewById(R.id.someOtherView).requestFocus();

      // FAIL 1
      InputMethodManager imm = (InputMethodManager)getSystemService(
        Context.INPUT_METHOD_SERVICE
      );
      imm.hideSoftInputFromWindow(v.getWindowToken(), 0);

      // FAIL 2
      MyActivity.this.getWindow().setSoftInputMode(
        WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN
      );

      return true;
    } else {
      return false;
    }
  }
}
mmBs
  • 8,421
  • 6
  • 38
  • 46
JoJo
  • 19,587
  • 34
  • 106
  • 162
  • Some solutions below have done the trick for me. But I'd like to note that it took me a while to solve this because I was using a ScrollView. Solution for that situation here: https://stackoverflow.com/questions/8100831/stop-scrollview-from-setting-focus-on-edittext – PhoenixB Jul 08 '18 at 01:52

8 Answers8

24

You could use a xml attribute,

android:cursorVisible 

or you can do it in code with this method.

 setCursorVisible(boolean).
coder_For_Life22
  • 26,645
  • 20
  • 86
  • 118
  • This fixed it. It also does the right thing by revealing the cursor when the keyboard is open. It only hides the cursor when you are not editing it. – JoJo Jan 14 '12 at 00:02
  • WIsh I could give more up votes as I found 10 other complicated answers that didn't work before finding this one. Thanks. – Glenn Dec 07 '12 at 22:42
  • I have this problem: http://stackoverflow.com/questions/23677895/saving-an-edittext-to-bitmap ----- With this solution,pin hidden too! But i wanna hide only vertical blink line. – Dr.jacky Apr 26 '15 at 10:10
  • 2
    uhm, it does indeed hides the cursor, but the Editext remains highlited (coloured) – frankelot Sep 19 '16 at 05:53
  • 9
    @JoJo if I use the `android:cursorVisible="false"` attribute it would remove the cursor in both, when the keyboard is open and when not. How did you get it to do what you wrote? And show the cursor only when you are editing it? – Daniele May 07 '17 at 16:18
  • 12
    This in any case should not be the answer! – Marjan Slavkovski Jan 26 '18 at 20:36
  • 1
    For me, it removes the blinking line completely, weather I have focus or not – nutella_eater Apr 28 '21 at 16:05
  • It not just doesn't resolve the problem but also creates another one.. – Dannark Oct 05 '21 at 18:09
21

Use the below code to remove the focus from the EditText

editText.setOnKeyListener(new OnKeyListener() {
            public boolean onKey(View view, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER) {
                    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
                    imm.hideSoftInputFromWindow(URLText.getWindowToken(), 0); 
                    editText.setFocusable(false);
                    editText.setFocusableInTouchMode(true);
                    return true;
                } else {
                    return false;
                }
            }
        });
dzirtbry
  • 328
  • 2
  • 14
Vinay Revankar
  • 823
  • 9
  • 19
10

Here is my custom EditText which detect whether keyboard is showing & automatically remove focus when keyboard is hidden

/**
 * Created by TheFinestArtist on 9/24/15.
 */
public class KeyboardEditText extends EditText {

    public KeyboardEditText(Context context) {
        super(context);
    }

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

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

    @Override
    public void setOnTouchListener(OnTouchListener l) {
        super.setOnTouchListener(l);
    }

    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect);
        if (listener != null)
            listener.onStateChanged(this, true);
    }

    @Override
    public boolean onKeyPreIme(int keyCode, @NonNull KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_UP) {
            if (listener != null)
                listener.onStateChanged(this, false);

            // Hide cursor
            setFocusable(false);

            // Set EditText to be focusable again
            setFocusable(true);
            setFocusableInTouchMode(true);
        }
        return super.onKeyPreIme(keyCode, event);
    }

    /**
     * Keyboard Listener
     */
    KeyboardListener listener;

    public void setOnKeyboardListener(KeyboardListener listener) {
        this.listener = listener;
    }

    public interface KeyboardListener {
        void onStateChanged(KeyboardEditText keyboardEditText, boolean showing);
    }
}
The Finest Artist
  • 3,150
  • 29
  • 34
9

After several attempts this is what worked best for me:

EditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
      @Override
      public boolean onEditorAction(TextView textView, int actionId,
          KeyEvent keyEvent) { //triggered when done editing (as clicked done on keyboard)
        if (actionId == EditorInfo.IME_ACTION_DONE) {
          editText.clearFocus();
        }
        return false;
      }
    });
Sharone Lev
  • 791
  • 7
  • 15
2

Fail 0:

Calling .requestFocus() on some layout element is not enough if the element is not focusable in touch mode. If you want to set the focus to a view or Button you have to call

.setFocusableInTouchMode(true);

first or set it in your .xml

Dominik
  • 406
  • 2
  • 12
0

For removing cursor

editText.isCursorVisible = false

And for gaining cursor visibilty when user click the edit text or it gets fouc cia imeOptions from other edit text, set this in onCreate

editText.setOnClickListener {
    binding.etFatherInLaw.isCursorVisible = true
}

editText.setOnFocusChangeListener(object:View.OnFocusChangeListener{
    override fun onFocusChange(view: View?, hasFocus: Boolean) {
      binding.etFatherInLaw.isCursorVisible = hasFocus
    }
})
Faizan Haidar Khan
  • 1,099
  • 1
  • 15
  • 20
-1

For Kotlin users... I borrowed the best solution from @Sharone Lev but I had to add some coroutine delay or, for some reason, the EditText that is clearing its focus will stop the keyboard from dismissing.

someEditText.setOnEditorActionListener { v, actionId, event ->

         when(actionId){
            EditorInfo.IME_ACTION_DONE->{

                //NOTE:wait few milliseconds before dismissing
                CoroutineScope(Dispatchers.IO).launch {
                    delay(500)
                    CoroutineScope(Dispatchers.Main).launch {
                        someEditText.clearFocus()
                    }
                }
            }
            else->{
                Log.w("TAG", "another action id ${actionId}")
            }
        }

        false
    }
MFAL
  • 1,090
  • 13
  • 19
-1

if you dont want it to be editable at all i'd say do the below;

EditText orgid; 
orgid.setText(user.getOrgId());
orgid.setEnabled(false);
Vivekanand
  • 755
  • 1
  • 8
  • 29