Chris Banes Chris Banes - 19 days ago 5
Android Question

Append Non-Editable String to EditText

Is there anyway to append a String to an EditText value, so that the string appended is not editable?

i.e. Say I have an EditText for Email Addresses, I want to treat an email address as a single entity rather than single characters. Therefore if a users deletes the last character of the email address, I want the whole email address to be removed rather than the last character.

I've been looking at Spannable but can't see anything obvious. This is similar to how Gmail and other apps do it.

Answer

Reposting my comment as an answer (and modifying the words to make my suggestion clearer).

Have an [onTextChanged](http://developer.android.com/reference/android/widget/TextView.html#onTextChanged(java.lang.CharSequence, int, int, int)) attached to your EditText to be notified of user input. You can then check the length of the contents of the EditText, and if it decreases by 1, then you can assume that the user deleted a character.

If the user entered me@sample.com then on your onTextChanged, you can set the text to an empty string. Of course you would have to do some checking first to make sure that the user has entered a complete email address before applying your change. Like, maybe make sure the user has entered the @ character already, or there is space after the last non-space character.