qwazix
qwazix

Reputation: 976

How to get the last character typed in a `EditText`?

I want to know when the user types a space character so that I know to update the undo list.

Using onKeyListener does not work on virtual keyboards (and is conceptually the wrong approach, I don't care how the user entered the space, he might have pasted it)

TextWatcher only provides the full text and taking the last character of the String does not help. The user might as well be typing in the middle. How can I get the last character inserted?

Upvotes: 3

Views: 3521

Answers (4)

slackwars
slackwars

Reputation: 522

Try this: It should take care of it for you. You may want to add a bit more error checking.

edittext.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(final CharSequence s, int start, int before, int count) {
        //Char at newly inserted pos.
        char currentChar = s.charAt(start + before);  
    }

    @Override
    public void afterTextChanged(Editable editable) {

    }
});

Upvotes: 7

xriminact
xriminact

Reputation: 73

Here is an answer to 'do something' when a '#'(special character) is entered in text.

editText.addTextChangedListener(new TextWatcher() {
        
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            //do nothing
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //do nothing
        }

        @Override
        public void afterTextChanged(Editable s) {
            String string = s.toString();
                if(string.contains("#")){
                    //do something
          }
        }

    });

Upvotes: 0

qwazix
qwazix

Reputation: 976

You can compare strings from before and after the TextChanged event so that you can see what has changed. This function returns the last char typed or deleted.

oldStr is set in beforeTextChanged and newStr in onTextChanged.

This additionally returns the last deleted character.

public char lastCharTyped(String oldStr, String newStr){
    String shorter, longer;
    //find which string is shorter so that we don't end up out of bounds
    if (oldStr.length()<newStr.length() ) {
        shorter = oldStr;
        longer = newStr;
    } else {
        shorter = newStr;
        longer = oldStr;
    }

    //find the first character that is different and return it
    for (int i = 0; i < shorter.length(); i++) {
        if (longer.charAt(i) != shorter.charAt(i)) return longer.charAt(i);
    }

    //if no characters are different then return the last char of the longer string
    //this means that the undo will be saved if the last char the user erased is a space
    Log.d("longer",longer);
    return(longer.charAt(longer.length()-1));
}

Upvotes: 1

Ad Dahoun
Ad Dahoun

Reputation: 483

et1.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

        // TODO Auto-generated method stub
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        // TODO Auto-generated method stub
    }

    @Override
    public void afterTextChanged(Editable s) {

        // TODO Auto-generated method stub
    }
});

Upvotes: -2

Related Questions