Auto-animate layout updates

Android offers preloaded animation that runs when you change the layout. Set an attribute in the layout to tell the Android system to animate these layout changes, and it carries out system-default animations for you.

Here's what a default layout animation looks like when adding items to a list:

Figure 1. Layout animation.

Create the layout

In your activity's layout XML file, set the android:animateLayoutChanges attribute to true for the layout that you want to enable animations for:

<LinearLayoutandroid:id="@+id/container"
android:animateLayoutChanges="true"
...
/>

Add, update, or remove items from the layout

Add, remove, or update items in the layout, and the items are animated automatically:

Kotlin

lateinitvarcontainerView:ViewGroup
...
privatefunaddItem(){
valnewView:View=...
containerView.addView(newView,0)
}

Java

privateViewGroupcontainerView;
...
privatevoidaddItem(){
ViewnewView;
...
containerView.addView(newView,0);
}

Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.

Last updated 2024年02月22日 UTC.