Search code examples
javaandroidandroid-studiotextwatcher

First character of the input text is always read by TextWatcher even when I've cleared my textfield completely


What I am trying to do is, to call another activity with the help of intent, if and only if my edittext field name is not empty.

My code:

Intent intent = new Intent(this, contacts.class);
        name.addTextChangedListener(new TextWatcher() {

            public void afterTextChanged(Editable s) {

            }

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

            }

            public void onTextChanged(CharSequence s, int start,
                                      int before, int count) {
                String text=name.getText().toString().trim();
                if(!text.isEmpty()) {
                    //println(s.toString());
                    Log.d("name",text.toString());
                    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(RadioGroup group, int checkedId) {
                            // checkedId is the RadioButton selected
                            RadioButton rb = (RadioButton) findViewById(checkedId);

                            if (checkedId != -1) {
                                if (rb.getText() == "custom") {
                                    intent.putExtra("FLAG", flag);
                                    startActivity(intent);
                                } else {
                                    flag = false;
                                    intent.putExtra("FLAG", flag);
                                    startActivity(intent);
                                }
                            }
                        }
                    });
                }
                else{
                    //println(s.toString());
                    Log.d("name",text.toString());
                    Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content), "Please type your name", Snackbar.LENGTH_LONG);
                    snackbar.show();
                }


            }
        });

When I run the code, after typing in something in my edittext field (name) and then deleting it completely, still allows the intent to call the new activity, which puzzles me.

So, I started debugging this with the help of Logcatand found out that the first character of the input that I give is never actually deleted and is readable by TextWatcher.

For example: Suppose I write Hello in my textfield, then my Logcat is like

H
He
Hel
Hell
Hello

This is fine. But, the problem arises when I start clearing the text. The characters get deleted like:

Hell
Hel
He
H

Even when my edittext field is completely empty, that first character of the string (H) is still read by TextWatcher. Why?


Solution

  • Currently the code to fire your intent is when you click one of the two RadioButton , so when you actually write something , you had just set a listener to that radio buttons , and let me tell you this , you edit text is actually empty and the logcat show you the last letter to exist in the EditText field which is H , after H it was empty so there are nothing to be printed in the logcat !

    You will get my point if you try to click the radio button without touching the EditText , it will not fire the intent , but if you write even a one letter , then the radio click listener will registered and ready to fire the intent !

    Now I will tell you what you have to do , first EditText listener :

    Intent intent = new Intent(this, contacts.class);
    

    And then set radio button click :

    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
         @Override
         public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(!TextUtils.isEmpty(name.getText.toString())){
                    // checkedId is the RadioButton selected
                    RadioButton rb = (RadioButton) findViewById(checkedId);
                          if (checkedId != -1) {
                               if (rb.getText() == "custom") {
                                  intent.putExtra("FLAG", flag);
                                  startActivity(intent);
                               } else {
                                  flag = false;
                                  intent.putExtra("FLAG", flag);
                                  startActivity(intent);
                              }
                          }
                }
           }
    });
    

    Enjoy !