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

Louis picture Louis · Jun 23, 2012 · Viewed 27.8k times · Source

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.

Answer

mikejonesguy picture mikejonesguy · Oct 19, 2012

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);
    }

}