0

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.

Amit Pandya
  • 361
  • 1
  • 3
  • 19

1 Answers1

0

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

Meenaxi
  • 567
  • 5
  • 17
  • I tried that too but it didnt work either. Everytime I start typing, it starts to call fadein animation. I also tried creating int variable like a =0 and after animation a++ and used a condition in text change listener that if a == 0 than start fadeIn animation. So it didn't work at all. – Amit Pandya Jun 19 '15 at 09:26
  • okay, so in that case its better to use setOnFocusChangeListener(). Actually i didn't understand your question properly. When you want your label to be fade in and when to fade out? – Meenaxi Jun 19 '15 at 09:33
  • I want to fade in my label when user clicks on associated edit text. Than I want my on focus change listener to make sure to hide label if that edit text is empty. Else label will be visible. – Amit Pandya Jun 19 '15 at 10:08