Home » Android » android – Click Listener behavior when two layouts get animated

android – Click Listener behavior when two layouts get animated

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have two buttons.When first button is pressed i am translating my layout layout to top and from the top of the screen another layout will come.But my issue is when came back to first layout the click events of the second layout still get fired at its previous position.So what is the Solution of it?I found a lot here on SO as well as on Google but still cannot get the right solution yet.So Please someone help me for my this issue.Thanks in Advance.

TranslateAnimation tr1 = new TranslateAnimation(0, 0, 0, -1100);
        tr1.setDuration(1000);
        tr1.setFillAfter(true);
        layout_login.startAnimation(tr1);
        tr1.setAnimationListener(new AnimationListener() {

            public void onAnimationStart(Animation animation) {

            }

            public void onAnimationRepeat(Animation animation) {

            }

            public void onAnimationEnd(Animation animation) {
                layout_signup.setVisibility(View.VISIBLE);
                TranslateAnimation tr2 = new TranslateAnimation(0, 0,
                        -1100, 0);
                tr2.setDuration(1000);
                tr2.setFillAfter(true);
                tr2.setInterpolator(MainActivity.this,
                        android.R.anim.linear_interpolator);
                layout_signup.startAnimation(tr2);

                tr2.setAnimationListener(new AnimationListener() {

                    public void onAnimationStart(Animation animation) {

                    }

                    public void onAnimationRepeat(Animation animation) {

                    }

                    public void onAnimationEnd(Animation animation) {
                        home_activity_btn_login.setEnabled(true);
                    }
                });
            }
        });
How to&Answers:

I think this is a bug with the old animator schemes (I beleive a pretty well known bug, involving fill after not working sometimes). Try using ObjectAnimator instead

Here is an example,

ObjectAnimator oa = ObjectAnimator.ofFloat(view, "translationX", 0, 100f);
oa.setDuration(1000);
oa.start();

If you want to move in the Y direction, you can use translationY. If you want to move in both directions, you need a translationX and translationY, and use an AnimatorSet to play simultaneously.

Check out this comment to this question. Using the old Animation API, apparently dispite fillAfter(true), the buttons click position remains the same. This confirms your issue. So just use the new API, and you should be in good shape.