I want to have constant text inside editText like:
http://<here_user_can_write>
User should not be able to delete any chars from "http://
", I searched about this and found this:
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) : "";
}
}
});
but I don't know whether it restricts user to not delete any chars from start to end limit. I also could not understand use of Spanned class.
One way would be a good choice if we can put a TextView
inside EditText
but I don't think it is possible in Android since both are Views, is it possible?
Use custom
EditText
View to draw the prefix text and add padding according to the prefix text size:This one is basically to add prefix "+91" to your edit text field of phone number.
1.Add this code to oncreate() of activity
2.Declare a method called getPhoneFilter()
3.declare "digits_number" in your values file
You had it almost right, try
CODE TO ADD CUSTOM PREFIX TO YOUR EDITTEXT (PREFIX NOT EDITABLE)
Code from Medium by Ali Muzaffar
And XML
There was a slight problem with @Rajitha Siriwardena's answer. It assumes that the entire string except the suffix has been deleted before the suffix is meaning if you have the string
and try to delete any part of
http://
you will deletestackoverflow.com/
resulting in onlyhttp://
.I also added a check incase the user tries to input before the prefix.
Note: this doesn't handle the case where the user tries to edit the prefix itself only before and after.