Button not responding to Click Event after Translation animation
Asked Answered
B

8

16

I have performed a Translation animation on button ,everything work as i excepted ,but only the problem is after the animation the button not responding to click event please correct me

Button b=(Button)findViewById(R.id.button1);
    TranslateAnimation slide=new TranslateAnimation(0, 30, 0,-40);
    slide.setDuration(500);
    slide.setZAdjustment(TranslateAnimation.ZORDER_TOP);
    slide.setFillAfter(true);

    b.startAnimation(slide);
Breuer answered 4/5, 2012 at 5:29 Comment(1)
seem, you forget to add listener to Button.Kappenne
B
22

If you are handling animations at lower level API (below HoneyComb) your animation actually does not moves the button but only its images.Its click will be at same place where you have placed it in your layout.

Bytom answered 4/5, 2012 at 5:33 Comment(3)
:Then after translation how can i change the button position ,currently i have set animation listener ,and in animation end callback i have take the layoutparam and set to it ,but it won't solve the problemBreuer
on animation is completed you will have to create a new button dynamically and add it to that position and change button visibility to view.gone.Bytom
or you can create two identical buttons in layout and switch their visibility on animation.Bytom
R
23

This will translate the object in y direction:

ObjectAnimator mover = ObjectAnimator.ofFloat(v, "translationY", 0, 400);
mover.start();
Rosebay answered 4/5, 2012 at 6:27 Comment(1)
Also you can combine translate animation in x and y direction using AnimatorSet.Rosebay
B
22

If you are handling animations at lower level API (below HoneyComb) your animation actually does not moves the button but only its images.Its click will be at same place where you have placed it in your layout.

Bytom answered 4/5, 2012 at 5:33 Comment(3)
:Then after translation how can i change the button position ,currently i have set animation listener ,and in animation end callback i have take the layoutparam and set to it ,but it won't solve the problemBreuer
on animation is completed you will have to create a new button dynamically and add it to that position and change button visibility to view.gone.Bytom
or you can create two identical buttons in layout and switch their visibility on animation.Bytom
T
10

I faced this issue and I did fix it just some second ago. So, I think that I should share my solution with you guys.

  • In animation xml file,I removed android:fillAfter="true" when keep android:fillEnabled="true".

  • Register Animation listener, then in onAnimationEnd() method, I call View#Layout() to change the position of the view.

                    int newLeft = (int) (layoutContent.getLeft() + layoutContent.getWidth() * 0.8);
                    layoutContent.layout(newLeft, 
                                        layoutContent.getTop(), 
                                        newLeft + layoutContent.getMeasuredWidth(), 
                                        layoutContent.getTop() + layoutContent.getMeasuredHeight());
    

In my case, what the animation do is that slides the layoutContent to leftside 80% of width.

It works fine. Hope this helps.

@Update: Today, you can use ObjectAnimator on android 3.0 +. If you are developing for android under 3.0, you can find it at support library v.4. ObjectAnimator is bester for animation.

@Update#2: You can use ViewPropertyAnimator on android api higher 12 version. It provides better performance, and fix problem with click events. Example:

mButton.animate()
                .setDuration(TIME)
                .translationY(VALUE)
                .start();
Toomay answered 13/6, 2012 at 11:11 Comment(3)
ObjectAnimator is not present in the support library!!Sloane
+1 HUGE help with "In animation xml file,I removed android:fillAfter="true" when keep android:fillEnabled="true" "Livy
mButton.animate() works flawlessly. It translates and moves the button as well. +1Lanctot
R
5

I could achieve what you wanted but you will have manage co-ordinates manually. See if it works for you.

public class AnimationTestActivity extends Activity {

    private Button mButton;

    private LinearLayout.LayoutParams params;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mButton = (Button) findViewById(R.id.button);
        mButton.setOnClickListener(mClickListener);

        params = (LayoutParams) mButton.getLayoutParams();
    }

    private android.view.View.OnClickListener mClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            TranslateAnimation animation = new TranslateAnimation(0, 0, 0, 400);
            animation.setDuration(2000);
            animation.setAnimationListener(mAnimationListener);
            v.startAnimation(animation);
        }
    };

    private AnimationListener mAnimationListener = new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            params.topMargin = params.topMargin + 400;
            mButton.setLayoutParams(params);
        }
    };
}
Rosebay answered 4/5, 2012 at 13:45 Comment(1)
Hi @Sushil, your solution actually works for me, even for both show/hide animations, but the inly issue I can see is the view flashes on the opposite side of the screen just in the moment the animation ends and it's "persisted" in its new position. Did you observ this as well?Proximo
C
4

The animation do not change the View actually Position. even if you setfillAfter(true), The position it can accept click event is the original position.

Concessionaire answered 4/5, 2012 at 5:31 Comment(0)
B
1

I found an easy solution define the button final position in layout and start the animation from some position ,ie specify the fromX or fromY instead of putting as zero

Breuer answered 13/6, 2012 at 16:10 Comment(1)
Does this mean there will be no accepted answer? You can accept your own answer too.Saffier
C
0

Try to use this:

b.layout(0, -40, b.getWidth(), b.getHeight());
Calypso answered 4/5, 2012 at 6:14 Comment(0)
S
0

ClickListener won't work, but you can use onTouchListener:

view.setOnTouchListener { _, event ->

    if (event.action == MotionEvent.ACTION_DOWN) {
        // call you click action here
        true // Consume the touch event
    } else {
        false // Pass the event to other listeners
    }
}
Semipro answered 4/7 at 16:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.