This example shows you how to create animation using the default XML tag in a gridview.
This application demonstrates how to use the animateLayoutChanges tag in XML to automate
transition animations as items are removed from or added to a container.
Click on the download link to get the source code.
Let’s have a look.
At first we will look at the layout XML.
layout_animations_by_default.xml
<? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" > < Button android:id = "@+id/addNewButton" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "Add Button" /> < GridLayout android:id = "@+id/gridContainer" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:animateLayoutChanges = "true" android:columnCount = "4" /> </ LinearLayout > |
Now the Activity that uses this XML to create the animation.
LayoutAnimationsByDefault.java
package com.example.animationdemo1; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.GridLayout; /** * This application demonstrates how to use the animateLayoutChanges tag in XML to automate * transition animations as items are removed from or added to a container. */ public class LayoutAnimationsByDefault extends Activity { private int numButtons = 1 ; @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.layout_animations_by_default); final GridLayout gridContainer = (GridLayout) findViewById(R.id.gridContainer); Button addButton = (Button) findViewById(R.id.addNewButton); addButton.setOnClickListener( new View.OnClickListener() { public void onClick(View v) { Button newButton = new Button(LayoutAnimationsByDefault. this ); newButton.setText(String.valueOf(numButtons++)); newButton.setOnClickListener( new View.OnClickListener() { public void onClick(View v) { gridContainer.removeView(v); } }); gridContainer.addView(newButton, Math.min( 1 , gridContainer.getChildCount())); } }); } } |
You can download the complete source code for the above post from here.