Ericson Willians
Ericson Willians

Reputation: 7845

Android: How to catch every touch-input of the Soft Keyboard?

nameInput.setOnEditorActionListener(new OnEditorActionListener() {        
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.????_?????_??????) {
                Integer inputLength2 = nameInput.getText().length();
                String realTimeText = inputLength2.toString();
                textView1.setText("Number of Characters: " + realTimeText);
            }
        return false;
        }
    });

nameInput is an object of the EditText type. I want to display on a TextView the number of the characters of the String created from the EditText in real-time. The principle is simple and in my mind it would work perfectly (All that I would need to do is to "intercept" the characters of the Soft Keyboard, in the exactly way I did up there), but the problem is:

http://developer.android.com/reference/android/view/inputmethod/EditorInfo.html

There is no constant available for that, which probably means that I'll need to do some obscure trick to solve the problem. Do you know how I could do it?

Upvotes: 1

Views: 1448

Answers (1)

Tushar
Tushar

Reputation: 8049

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

You will be notified of any changes in the input. Example:

private TextWatcher textWatcher = new TextWatcher() {
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        Integer inputLength2 = s.length();
        String realTimeText = inputLength2.toString();
        textView1.setText("Number of Characters: " + realTimeText);
    }

    public void afterTextChanged(Editable s) {}
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
};

editText.addTextChangedListener(textWatcher);

Upvotes: 3

Related Questions