0

I am trying to retrieve which key was pressed inside an EditText view and set it as text of a TextView in the same activity. For example, if the last key pressed was the letter 't', then I want 't' to be displayed in the TextView (and not the entire character sequence).

I tried the following:

phoneInput.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        tracker.setText(s.charAt(start));
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
});

This resulted in a crash. I am not sure but it looks like start is index of the character that was appended when last key press occurred, so TextView should display the last character entered in EditText?

I tried this too:

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

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        String sequence = s.toString();
        tracker.setText(sequence.charAt(sequence.length() - 1));
    }

    @Override
    public void afterTextChanged(Editable s) {}
});

I guess use of any String method is causing a crash, even the length(). What am I doing wrong?

2 Answers2

1

You have to try like this

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

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                textView.setText(String.valueOf(charSequence.charAt(i1)));
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
Raja
  • 2,775
  • 2
  • 19
  • 31
  • In your solution, when the backspace is "typed", the application crashes. Also, it only shows the first letter that was entered in the EditText. Subsequent keypress don't change the value of TextView. –  Nov 29 '17 at 06:52
1

Use below code in call back of onTextChanged()

  String text = charSequence.toString();
            if (text.length() >0) {
                Character character = text.charAt(text.length()-1);
                Character character1 = character;
                tv.setText(character1.toString());
            } else {
                // if you want to remove last character too then setText(""");
            }

Hope that helps.

Abdul Waheed
  • 4,540
  • 6
  • 35
  • 58
  • Yes! it did the trick. But can you explain it further? Why the crash and why your solution worked? –  Nov 29 '17 at 06:42
  • Can you please explain the crash? –  Nov 29 '17 at 06:49
  • 1) Firs line converts the code to string it is very straight forward 2) Second line is for getting the last character charAt return character so I got that in character reference. if you get the character and save in an integer variable that means you are getting ascii code of that caracter and saving it int variable . and if you get that character with that int that is obvious that is wrong case. you need to convert that ascii code to the actual character and after that I converted that character to (you can do that without converting as well) and set that string on text view. – Abdul Waheed Nov 29 '17 at 06:51
  • hope I answer your question if not please feel free to ask – Abdul Waheed Nov 29 '17 at 06:52
  • Still, but, while clearing the last character (by clicking and holding the backspace or hitting backspace when only one character left to remove), my app crashes. I think I can fix that. I will ask again if I couldn't. Also, I think the 3rd line in your solution is redundant. Thanks! –  Nov 29 '17 at 06:57