Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove focus from EditText when user is done editing?

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;
    }
  }
}
like image 847
JoJo Avatar asked Jan 07 '12 02:01

JoJo


People also ask

How do I remove focus in EditText?

Remove focus but remain focusable: editText. setFocusableInTouchMode(false); editText. setFocusable(false); editText.

How do you make EditText read only?

The most reliable way to do this is using UI. setReadOnly(myEditText, true) from this library. There are a few properties that have to be set, which you can check out in the source code.


4 Answers

You could use a xml attribute,

android:cursorVisible 

or you can do it in code with this method.

 setCursorVisible(boolean).
like image 57
coder_For_Life22 Avatar answered Oct 08 '22 07:10

coder_For_Life22


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;
                }
            }
        });
like image 39
Vinay Revankar Avatar answered Oct 08 '22 07:10

Vinay Revankar


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);
    }
}
like image 10
The Finest Artist Avatar answered Oct 08 '22 09:10

The Finest Artist


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;
      }
    });
like image 7
Sharone Lev Avatar answered Oct 08 '22 07:10

Sharone Lev