Gtg
Gtg

Reputation: 3

Android EditText TextChanged

I want to start new activity when edittext change (edittext length = 6). I use my code below but didn't work. Help me please.

    a = (EditText) findViewById(R.id.editText1);
    a.addTextChangedListener(new TextWatcher() {
        @Override
        public void afterTextChanged(Editable s) {
            if(!s.equals("6") )
                    i = new Intent(getApplicationContext(), MainActivity.class);
                    startActivity(i);
                    finish();                
        }

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

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {

        }
       });

Upvotes: 0

Views: 170

Answers (4)

sanky jain
sanky jain

Reputation: 873

update

 if(!s.equals("6") )

to 

 if(s.length()==6 )

Upvotes: 0

Vivek Bhardwaj
Vivek Bhardwaj

Reputation: 528

Try this piece of code

a.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) {

                if(s.length() == 6)
                {
                    Intent in = new Intent(CurrentActivity.this,TargetActivity.class);
                    startActivity(in);
                }

            }
        });

Let me know if this works for you! :)

Upvotes: 0

Shree Krishna
Shree Krishna

Reputation: 8562

I am afraid how you have checked the length, Just replace your code with this in afterTextChanged

 @Override
        public void afterTextChanged(Editable s) {
            if(a.getText().length()== 6 ) {
                Intent i = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(i);
                finish();
            }
        }

Upvotes: 2

Ajinkya
Ajinkya

Reputation: 1077

Try this

a.addTextChangedListener(new TextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {

            }

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

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                        if(count==6 ){
                        i = new Intent(getApplicationContext(), MainActivity.class);
                        startActivity(i);
                        finish();  
            }
            }
           });

Upvotes: 0

Related Questions