Does anyone know how to disable the blinking cursor in an EditText
view?
The problem with setting cursor visibility to true and false may be a problem since it removes the cursor until you again set it again and at the same time field is editable which is not good user experience.
so instead of using
setCursorVisible(false)
just do it like this
editText2.setFocusableInTouchMode(false)
editText2.clearFocus()
editText2.setFocusableInTouchMode(true)
The above code removes the focus which in turn removes the cursor. And enables it again so that you can again touch it and able to edit it. Just like normal user experience.
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;
}
});
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);
}
In kotlin your_edittext.isCursorVisible = false
simple add this line into your parent layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true">
<EditText
android:inputType="text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
rootLayout.findFocus().clearFocus();