Android on focus change listener is not working properly

1.4k views Asked by At

I am working on an Android app and I am trying to implement a feature that I have found online. I am tying to do is when user clicks on edit text, for example, if user clicks on "Username" edit text, the setOnFocusChangeListener will check things and will start animation for label to fadeIn and fadeOut, here my codes

userSignupPasswordET.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (userSignupPasswordET.hasFocus()) {
                    signupPassWDTV.startAnimation(fadeIn);
                } else {
                    if (userSignupPasswordET.getText().toString().length() == 0) {
                        signupPassWDTV.startAnimation(fadeOut);
                    }
                }
            }
        });

The feature I am trying to do is here, on youtube. But the problem I am facing is that I have three fields, username, password and email. So if I click on username it is working and fadIn animation for username label works and also if I do not enter anything fadeOut animation works when I move to other edit text and the other edit text's own setOnFocusChangeListener works. But problem is the if I don't enter anything in any of them just keep switching them, keep changing their focus, the other two edit text's setOnFocusChangeListener gets triggered and starts fadeIn animation and than fadeOut animation. So I don't know what to do. Any suggestion? Remember, if I don't enter anything in edit field and just keep changing/toggling them, this problem is getting triggered. If I enter something in one, it changes and label stays visible.

1

There are 1 answers

3
Meenaxi On

You can add your logic on TextChangedListener()

edittext.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {

        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1,
                int arg2, int arg3) {
        }

        @Override
        public void afterTextChanged(Editable et) {

        }
    });

so that your animation get called only when you enter something on your edittext