I am using an EditText
. Is it possible to have a part of text uneditable and the rest editable in the same EditText
?
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.
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.
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.
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With