Harpreet Singh
Harpreet Singh

Reputation: 562

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.

Upvotes: 5

Views: 17251

Answers (4)

Meghal Agrawal
Meghal Agrawal

Reputation: 177

You can add a textListener for it . Define textListener in your onCreate !

For Example i have made this to check if user have entered a correct email !

        input_Mobile.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
            checkmobile=input_Mobile.getText().toString();
            mobilelength=checkmobile.length();



            if(ss.contentEquals("")){

                b1.setEnabled(false);

            }
            if(mobilelength==10){

                b1.setEnabled(true);

                }else {
                b1.setEnabled(false);


                }

        }   

In Addition to that there are three functions available .

  1. onTextChanged
  2. beforeTextChanged
  3. afterTextChanged

you can take use of that according to what you need .

Hope it helps you

Thanks .

Upvotes: 1

Aleksandr
Aleksandr

Reputation: 4936

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
            }
        }
    });

Upvotes: 10

Anoop M Maddasseri
Anoop M Maddasseri

Reputation: 10569

     titleBox.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) {
                    int ssidLength = charSequence.length();
                    if (ssidLength == 0) {
                        return;
                    }
              if (charSequence.charAt(ssidLength - 1)==' ')
               {
                //Do stuff here
               }
                }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });

Upvotes: 0

Sheychan
Sheychan

Reputation: 2436

edittext.setOnKeyListener(new OnKeyListener(){

 @Override 
public boolean onKey(View v, int keyCode, KeyEvent event) { 
    if ((event.getAction() == KeyEvent.ACTION_DOWN) && 
            (keyCode == KeyEvent.KEYCODE_SPACE)) { 

        //do code

      return true;

        }        
       return false; 
    }
});

Upvotes: 4

Related Questions