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;
}
}
}
Remove focus but remain focusable: editText. setFocusableInTouchMode(false); editText. setFocusable(false); editText.
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.
You could use a xml attribute,
android:cursorVisible
or you can do it in code with this method.
setCursorVisible(boolean).
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;
}
}
});
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);
}
}
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;
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With