Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 790
Sam97305421562 Avatar asked May 26 '09 11:05

Sam97305421562


People also ask

How do I edit EditText Uneditable?

If you want your disabled EditText to look the same as your enabled one, you should use android:enabled="false" in combination with android:textColor="..." . Show activity on this post. Used this if you have an icon to be clickable, this works for me.

How do you make EditText not editable and clickable?

For the above requirement the solution in XML is android:editable="false" but I want to use this in Java. et. setKeyListener(null); It makes the EditText not EDITABLE but at the same time it makes it non clickable as well.

How do you make EditText read only?

The most reliable way to do this is using UI. setReadOnly(myEditText, true) from this library. There are a few properties that have to be set, which you can check out in the source code.

How do I turn off edit text on android?

What should I set to disable EditText? It's possible to preserve both the style of the view and the scrolling behaviour. To disable an EditText while keeping this properties, just use UI. setReadOnly(myEditText, true) from this library.


2 Answers

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.

like image 172
Josef Pfleger Avatar answered Oct 12 '22 13:10

Josef Pfleger


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);
like image 41
Ulrich Scheller Avatar answered Oct 12 '22 12:10

Ulrich Scheller