Home » Android » TranslateAnimated ImageView not clickable after animation [Android]

TranslateAnimated ImageView not clickable after animation [Android]

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have 2 ImageViews that I translate from the top of the screen to the bottom. these views are infalted from xml and the animation is added from java code. The animation works perfectly, but the onClickListener I added in java code doesn’t seem to work. I used fillAfter attribute of the animation to make the iamges stay at their arrival after the translation, but THESE images aren’t clickable anymore… However, their position before translation remains clickable…
I can’t see the logic of this. Could anyone give me some piece of advice about that?

How to&Answers:

This is because Animations affects only the drawing of widget. However, The real Location is not affected -It is still in the previous one-.

To overcome this problem, you need to update the layout parameters of the ImageView manually by installing an animation listener as follows:

Animation.setAnimationListener(new AnimationListener() {
        public void onAnimationStart(Animation arg0) {

        }

        public void onAnimationRepeat(Animation arg0) {
            //TODO Auto-generated method stub
        }

        public void onAnimationEnd(Animation arg0) {
            android.widget.LinearLayout.LayoutParams params = new LayoutParams(
            android.widget.LinearLayout.LayoutParams.FILL_PARENT,
            android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);
            params.topMargin = addLocationButton.getTop()-100;

            ImageView.setLayoutParams(params);
        }
        });