Search code examples
androidtexttextviewkeypressselectall

Is it possible to ignore a special character when all text is selected in EditText?


I have a EditText. When I focus on it, all of the text is selected. When the user enters a character, the previous text (selected text) is removed and the new character is inserted in the EditText.

I want to prevent removing the selected text when the user inserts character '='.

How can I do that?


Solution

  • Use a textwatcher with your EditText , it has three methods

    afterTextChanged(Editable s)
    beforeTextChanged(CharSequence s, int start, int count, int after)
    onTextChanged(CharSequence s, int start, int before, int count)
    

    Use beforeTextChanged and check if the special character( = or whatever) has been inserted , keep old text

    http://developer.android.com/reference/android/text/TextWatcher.html