How to disable cursor positioning and text selection in EditText? (Android)

I am looking for a way to prevent the user from moving the cursor position anywhere. The cursor should always remain at the end of the current EditText value. In addition to this, the user does not have to select anything in the EditText. Do you know how to implement this in Android using EditText?

To clarify: the user should be able to insert text, but only at the end.

+23
android textselection android-edittext android-cursor
Jun 23 2018-12-12T00:
source share
4 answers

I had the same problem. It ended up working for me:

public class CustomEditText extends EditText { @Override public void onSelectionChanged(int start, int end) { CharSequence text = getText(); if (text != null) { if (start != text.length() || end != text.length()) { setSelection(text.length(), text.length()); return; } } super.onSelectionChanged(start, end); } } 
+31
Oct 19
source share

This will reset the focus cursor to the last position of the text.

 editText.setSelection(editText.getText().length()); 

This method will disable cursor movement when touched.

 public class MyEditText extends EditText{ @Override public boolean onTouchEvent(MotionEvent event) { final int eventX = event.getX(); final int eventY = event.getY(); if( (eventX,eventY) is in the middle of your editText) { return false; } return true; } } 

And you can use either the xml attribute

Android: cursorVisible

or java function

setCursorVisible (boolean)

to disable the flashing edittext cursor

+6
Jun 23 '12 at 15:04
source share

It seems like the best way to do this is to create your own CustomEditText class and override / modify any relevant methods. You can see the source code for EditText here.

 public class CustomEditText extends EditText { @Override public void selectAll() { // Do nothing } /* override other methods, etc. */ } 
+1
Jun 23 '12 at 16:17
source share

Try the following:

 mEditText.setMovementMethod(null); 
+1
Jun 12 '14 at 9:04 on
source share



All Articles