Reputation: 20115
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;
}
}
}
Upvotes: 30
Views: 46701
Reputation: 1215
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
}
})
Upvotes: 0
Reputation: 1110
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
}
Upvotes: -1
Reputation: 841
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;
}
});
Upvotes: 11
Reputation: 832
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;
}
}
});
Upvotes: 21
Reputation: 3189
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);
}
}
Upvotes: 11
Reputation: 396
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
Upvotes: 2
Reputation: 755
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);
Upvotes: -1
Reputation: 26971
You could use a xml attribute,
android:cursorVisible
or you can do it in code with this method.
setCursorVisible(boolean).
Upvotes: 22