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

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

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

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

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

  5. Hi sir, I am receiving this message: java.lang.NoSuchMethodException: measure [int, int]

    From here: Method m = v.getClass().getDeclaredMethod(“onMeasure”, int.class, int.class);

    Your code:

    try {
    Method m = v.getClass().getDeclaredMethod(“onMeasure”, int.class, int.class);
    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
    View.MeasureSpec.makeMeasureSpec(((View) v.getParent()).getMeasuredWidth(), View.MeasureSpec.AT_MOST)
    } catch (Exception e) {

    Do you know why is this happening?

    1. @deneb: hi. as my post indicated, this is a hack. It may be that the API you are using now does not have this method anymore. Using Reflection is not always a solution unless you yourself made the methods. In this case, it is Google so they probably removed the method in later APIs and moved it to some other class which is why you are getting the exception now.

Leave a Reply

Your email address will not be published. Required fields are marked *