Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KeyListener on EditText : Android

I have a editText in my app and i want to run some code when the user use (space) into it.So, please give me a little bit of idea on how to do that.

like image 303
Harpreet Singh Avatar asked Dec 09 '22 02:12

Harpreet Singh


1 Answers

You don't need KeyListener. Use TextWatcher for it:

((EditText)findViewById(R.id.your_edit_text)).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) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if(s != null && s.length() > 0 && s.charAt(s.length() - 1) == ' '){
                //dp something
            }
        }
    });
like image 100
Aleksandr Avatar answered Dec 11 '22 10:12

Aleksandr