How to expand view smoothly in Android?

wayne_bai picture wayne_bai · Nov 6, 2012 · Viewed 15.9k times · Source

I met an issue when I expanded a 320*50 view to full-screen.

If I expand it directly, it works but the action is very abrupt and I think is not a good user experience. So I hide the view first, then expand it, and after two second, show the view again.

TextView.setVisibility(VIEW.INVISIBLE);
ViewGroup.LayoutParams lp = TextView.getLayoutParams();
lp.width = ViewGroup.LayoutParams.FILL_PARENT;
lp.height = ViewGroup.LayoutParams.FILL_PARENT;

//after two seconds
handler.postDelay(new Show(),2000);

class Show implements Runnable{
   @Override
public void run(){
       TextView.setVisibility(VIEW.VISIBLE);
   }
}

So l left two seconds for the application to expand the view. Then after two seconds the view will show again. I am expected that the view is expanded to full-screen when it shows. But actually, it doesn't. The view do the expand action after it shows instead of during the two seconds it is hiding.

Answer

Sam Chen picture Sam Chen · Jan 9, 2020
  1. Add android:animateLayoutChanges="true" to your ViewGroup.
  2. Use setVisibility() to control the visibility of your target View.
  3. If there is other View below you target View, add android:animateLayoutChanges="true" to your outer ViewGroup and following code before setVisibility():

    LayoutTransition layoutTransition = rootLinearLayout.getLayoutTransition();
    layoutTransition.enableTransitionType(LayoutTransition.CHANGING);