Move view to "layout_alignparentend" with animation

Viewed 50

I have a relative layout in which I created a CardView which is half in width of its parent. I just want to move CardView to the end of it's parent with animation. Here is my code:

            TranslateAnimation animation = new TranslateAnimation(0, 180, 0, 0);
            animation.setDuration(600);
            animation.setFillAfter(true);
            backCard.startAnimation(animation);

This code is working perfectly on some of screens but not on others. Is there any way to move cardview to the end of it's parent relative layout with animation?

1 Answers

You will have to get the coordinates of where your RelativeLayout's end is. This will be different depending on your screen size. For that you have to provide your RelativeLayout with an @+id and get it in Java with findViewById() as any other View. Once you have it, you simply animate the view to these coordinates. Something like :

int parentEnd = relativeLayout.getWidth();

backCard.animate().translationX(parentEnd).setDuration(600);

This should get you started.

maybe you will have to tweak the value a little bit i.e.

translationX(parentEnd - (backCard.getWidth() / 2))

, because other wise it will animate the middle of your CardView to the end of your RelativeLayout, which should be looking like it is half cut off to the right.

Related