Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm searching for a way to prevent the user from moving the cursor position anywhere. The cursor should always stay at the end of the current EditText value. In addition to that the user should not be able to select anything in the EditText. Do you have any idea how to realize that in Android using an EditText?

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

like image 503
Louis Avatar asked Jun 23 '12 14:06

Louis


1 Answers

I had the same problem. This 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);     }  } 
like image 152
mikejonesguy Avatar answered Sep 20 '22 19:09

mikejonesguy