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...
  1. 10 Responses to “Android Animation To Expand & Collapse View & Its Children”

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

    By Ranjan Deo on Oct 4, 2012

  3. 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.

    By Nate Vogt on Dec 12, 2012

  4. @nate vogt: you’re welcome

    By admin on Dec 13, 2012

  5. 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.

    By Rajendra Singh on Jan 9, 2013

  6. @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

    By admin on Jan 9, 2013

  7. 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.

    By Rajendra Singh on Jan 10, 2013

  8. thanks, Now my problem resolve

    By Rajendra Singh on Jan 10, 2013

  9. @rajendra: sorry. got no time anymore

    By admin on Jan 10, 2013

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

    By admin on Jan 10, 2013

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

    By Rajendra Singh on Jan 18, 2013

Post a Comment