Android Animation To Expand & Collapse View & Its Children

Most expand and collapse animation code I saw in Android related forums never worked. I think that they probably are limited to just widgets. When the View object itself e.g. LinearLayout has children within it, then those animation XMLs or code to animate them during runtime do not work.

The closest code that worked is from Tom Esterez. He describes it as a dirty hack but I think that it is the only code that is shared publicly that works. However, the code he shared is limited to expanding the ViewGroup.

I modified his existing code to also cater a collapse version. The method returns Animation object.

Feel free to change the animation speed according to your preference. The lower the value, the faster the animation speed will be.

Expand the code area section to view the method in full.

Related Posts Plugin for WordPress, Blogger...
  • Ranjan Deo

    Thank you so much.
    This is exactly what I was looking for. :)

  • http://www.willowtreeapps.com Nate Vogt

    Thank you so much! This is ALMOST perfect.. The order of args in m.invoke() is switched around, so you are giving an unspecified width instead of height. As it is, this will work fine (so far in my testing, at least), until you have something like wrapping text that will change height depending on the width. But just swap those two lines and you’re golden.

    • admin

      @nate vogt: you’re welcome

  • Rajendra Singh

    Thanks for giving such a beautiful code, I am expanding/ collapsing view on the base of boolean flag, expanding work fine but on collapsing some time it take very much time to animate view. Please help.

    • admin

      @rajendra: thank you. maybe you have many components in there that it takes a bit of a lag to animate? i have not tried it with so many components though

  • Rajendra Singh

    thanks for reply, can you please help me how to solve this problem? I have a layout which contains 4 views(Button). I am including this layout in main layout and in main layout button click implementing this expand/collapse animation depend on the boolean flag.

  • Rajendra Singh

    thanks, Now my problem resolve

  • admin

    @rajendra: sorry. got no time anymore

  • admin

    @rajendra: if you solved it, please post your solution here, so others can benefit as well

  • Rajendra Singh

    Initailly I was applying view.setAnimation(Animation an) which occurred delay, By using view.startAnimation it’s working perfect.