GridView RandomFade animation in Android

By | May 1, 2013

In some of my older posts I have already showed a lot of animations on ListView and GridViews. Search coderzheaven for ListView and GridView animations if you want to see the articles and posts.

This is also yet another post on animation. Here what I will do is create an animation in a GridView such that all cells will appear “RANDOM” with a fade animation.

GridView Random Animation
GridView Random Animation

So we will start

Click on the download link to download the code.

We will start with the animation XML first.

First create a folder named “anim” inside the “res” folder of your project.
Inside it create fade.xml and copy this code into it.


<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android=""
       android:fromAlpha="0.0" android:toAlpha="1.0"
       android:duration="@android:integer/config_longAnimTime" />

Then create anotther xml file named “layout_random_fade.xml” and copy this code into it.

<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android=""
        android:animation="@anim/fade" />

This is the factor that creates the random animation.

And this one creates the fade animation.

Now we will write the activity that uses the gridview to create the animation.

package com.coderzheaven.gridviewrandomfadeanimation;

import java.util.List;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class MainActivity extends Activity {
	public void onCreate(Bundle savedInstanceState) {


		GridView grid = (GridView) findViewById(;
		grid.setAdapter(new AppsAdapter());


	private List<ResolveInfo> mApps;

	private void loadApps() {
		Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);

		mApps = getPackageManager().queryIntentActivities(mainIntent, 0);

	public class AppsAdapter extends BaseAdapter {
		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView i = new ImageView(MainActivity.this);

			ResolveInfo info = mApps.get(position % mApps.size());

			final int w = (int) (36 * getResources().getDisplayMetrics().density + 0.5f);
			i.setLayoutParams(new GridView.LayoutParams(w, w));
			return i;

		public final int getCount() {
			return Math.min(32, mApps.size());

		public final Object getItem(int position) {
			return mApps.get(position % mApps.size());

		public final long getItemId(int position) {
			return position;

Ok Now it’s done. Run the project and see the result.

You can download the complete java android sample code from here.

One thought on “GridView RandomFade animation in Android

  1. Pingback: Wave Scale Animation in GridViews in Android

Leave a Reply

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