Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Animate views that are programatically added to the layout

I am creating a couple of TextViews programmatically and adding to a Linear Layout.Everything works fine.

Now I want to add some animations on the TextViews. After adding the textviews I am trying to add animations on them using the ViewPropertyAnimator. But I don't see any animations. If I add them in the xml for testing, it works fine.

I found some thing like we need to add an event for when the textview is attached to the screen and code the animation here. But I have a set of animations to be run on each TextView and after it is done I need to start animations on the other.

Any ideas or pointers how I can achieve this?

My code is some thing like,

-- Create the TextViews(invisible) in a loop and add them to the layout.
-- After the above step trying to animate like below each of the text views one by one. I need to chain as I need to fade in one by one.

        tv1.setVisibility(View.VISIBLE);
        tv1.animate().alpha(1).setDuration(500);

But the animations are not working.

Thanks for any help.

like image 420
kiran Avatar asked Jun 24 '26 18:06

kiran


1 Answers

The ViewPropertyAnimator animates properties from their current value to a specified value. Since the alpha of newly instantiated Views is 1, your animation will animate alpha from 1 to 1. You should first set the alpha to 0 rather than setting the visibility:

tv1.setAlpha(0);
tv1.animate().alpha(1).setDuration(500);
like image 74
tachyonflux Avatar answered Jun 30 '26 17:06

tachyonflux



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!