Here below code is use full to create List View with Animation.
Android Activity Class(ListviewAnimation.java)
package com.androidsurya.listview;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class ListviewAnimation extends Activity {
private ListView mainListView;
private DisplayMetrics metrics;
MyOwnAdapater moAdapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
mainListView = new ListView(this);
mainListView.setFadingEdgeLength(0);
String[] atoz = new String[] { "A", "B", "C", "D", "E", "F", "G", "H",
"I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
"U", "V", "W", "X", "Y", "Z" };
ArrayList<String> strings = new ArrayList<String>();
for (int i = 0; i < atoz.length; i++) {
strings.add(atoz[i]);
}
moAdapter = new MyOwnAdapater(this, strings, metrics);
mainListView.setAdapter(moAdapter);
setContentView(mainListView);
}
public class MyOwnAdapater extends ArrayAdapter<String> {
private Context context;
private LayoutInflater mInflater;
private ArrayList<String> strings;
private DisplayMetrics metrics_;
private class Holder {
public TextView textview;
}
public MyOwnAdapater(Context context, ArrayList<String> strings,
DisplayMetrics metrics) {
super(context, 0, strings);
this.context = context;
this.mInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.strings = strings;
this.metrics_ = metrics;
}
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
final String str = this.strings.get(position);
final Holder holder;
if (convertView == null) {
convertView = mInflater.inflate(
android.R.layout.simple_list_item_1, null);
convertView.setBackgroundColor(0xFF202020);
holder = new Holder();
holder.textview = (TextView) convertView
.findViewById(android.R.id.text1);
holder.textview.setTextColor(0xFFFFFFFF);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
holder.textview.setText(str);
Animation animation = null;
animation = new TranslateAnimation(metrics_.widthPixels / 2, 0, 0,
0);
animation.setDuration(750);
convertView.startAnimation(animation);
animation = null;
return convertView;
}
}
}
Register Activity in Android Manifest File
<activity
android:name="com.androidsurya.listview.ListviewAnimation"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Output Screenshot:
Android Activity Class(ListviewAnimation.java)
package com.androidsurya.listview;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class ListviewAnimation extends Activity {
private ListView mainListView;
private DisplayMetrics metrics;
MyOwnAdapater moAdapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
mainListView = new ListView(this);
mainListView.setFadingEdgeLength(0);
String[] atoz = new String[] { "A", "B", "C", "D", "E", "F", "G", "H",
"I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
"U", "V", "W", "X", "Y", "Z" };
ArrayList<String> strings = new ArrayList<String>();
for (int i = 0; i < atoz.length; i++) {
strings.add(atoz[i]);
}
moAdapter = new MyOwnAdapater(this, strings, metrics);
mainListView.setAdapter(moAdapter);
setContentView(mainListView);
}
public class MyOwnAdapater extends ArrayAdapter<String> {
private Context context;
private LayoutInflater mInflater;
private ArrayList<String> strings;
private DisplayMetrics metrics_;
private class Holder {
public TextView textview;
}
public MyOwnAdapater(Context context, ArrayList<String> strings,
DisplayMetrics metrics) {
super(context, 0, strings);
this.context = context;
this.mInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.strings = strings;
this.metrics_ = metrics;
}
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
final String str = this.strings.get(position);
final Holder holder;
if (convertView == null) {
convertView = mInflater.inflate(
android.R.layout.simple_list_item_1, null);
convertView.setBackgroundColor(0xFF202020);
holder = new Holder();
holder.textview = (TextView) convertView
.findViewById(android.R.id.text1);
holder.textview.setTextColor(0xFFFFFFFF);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
holder.textview.setText(str);
Animation animation = null;
animation = new TranslateAnimation(metrics_.widthPixels / 2, 0, 0,
0);
animation.setDuration(750);
convertView.startAnimation(animation);
animation = null;
return convertView;
}
}
}
Register Activity in Android Manifest File
<activity
android:name="com.androidsurya.listview.ListviewAnimation"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Output Screenshot: