Sam97305421562
Sam97305421562

Reputation: 3037

Can we have uneditable text in edittext

I am using an EditText. Is it possible to have a part of text uneditable and the rest editable in the same EditText?

Upvotes: 18

Views: 10935

Answers (2)

Ulrich Scheller
Ulrich Scheller

Reputation: 11910

You can implement a TextChangedListener where you make sure those parts of your text wont get deleted/overwritten.

class TextChangedListener implements TextWatcher {
    public void afterTextChanged(Editable s) {
                makeSureNothingIsDeleted();
    }

    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

    public void onTextChanged(CharSequence s, int start, int before, int count) {}
}

    TextChangedListener tcl = new TextChangedListener();
    my_editable.addTextChangedListener(tcl);

Upvotes: 2

Josef Pfleger
Josef Pfleger

Reputation: 74527

You could use

editText.setFocusable(false);

or

editText.setEnabled(false);

although disabling the EditText does currently not ignore input from the on-screen keyboard (I think that's a bug).

Depending on the application it might be better to use an InputFilter that rejects all changes:

editText.setFilters(new InputFilter[] {
    new InputFilter() {
        public CharSequence filter(CharSequence src, int start,
            int end, Spanned dst, int dstart, int dend) {
            return src.length() < 1 ? dst.subSequence(dstart, dend) : "";
        }
    }
});

Also see this question.

Upvotes: 16

Related Questions