In the below example I have created ListView animation in android. Here, first I have created ListView and Button in activity_main.xml layout and also added animation layout with in ListView. Then in Second step I have created list_anim layout with in anim Directory. In third step I have created a new scale.xml layout with in anim Directory. Now see programming area at last, In MainActivity I have used loadScreen() ,SetupListView method. You can see below example code it clearly describe you How to make ListView animation in android.
Step(1)activity_main.xml layout-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/myListView"
android:persistentDrawingCache="animation|scrolling"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layoutAnimation="@anim/list_anim" />
<Button
android:id="@+id/myRestartButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Click here" />
</LinearLayout>
Step(2)- I have created list_anim.xml layout with in anim Directory-
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="50%"
android:animation="@anim/scale" />
Step(3)- In third step I have created a new scale_anim.xml layout with in anim Directory-
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<scale
android:fromXScale="0.1"
android:toXScale="1"
android:fromYScale="0.1"
android:toYScale="1.0"
android:duration="2000"
android:pivotX="10%"
android:pivotY="10%"
android:background="#e6e2c6"
android:startOffset="100" />
</set>
Step(4)-MainActivity-
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loadScreen();
}
private Button.OnClickListener MyRestartButtonOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View v) {
loadScreen();
}
};
private void loadScreen(){
setContentView(R.layout.activity_main);
SetupListView();
Button MyRestartButton = (Button)findViewById(R.id.myRestartButton);
MyRestartButton.setOnClickListener(MyRestartButtonOnClickListener);
}
private void SetupListView()
{
String[] listItems = new String[] {
"Hello Evon devlopers!",
"Rajshekhar Tiwari",
"Rohit",
"Inder Tiwari",
"Siva"
};
ArrayAdapter<String> listItemAdapter
= new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
listItems);
ListView lv = (ListView)this.findViewById(R.id.myListView);
lv.setAdapter(listItemAdapter);
}
}
1 Comment(s)