Android listview slide animation

In the previous tutorial, you learnt how to define the slideshow animation in xml file and apply the animation to an ImageView. Besides the ImageView, the animation can be applied to other views. In this Android, I am going to show you how to set the slide animation to a ListView.


Now to have a workable example Android application to demonstrate the ListView slide animation, you create a new Android project. Then modify the activity_main.xml file to add an ListView. Here is the content of the activity_main.xml file after the ListView was added.


<LinearLayout xmlns:android=" http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:id="@+id/container"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   android:paddingBottom=" @dimen/activity_vertical_margin"
   android:paddingLeft=" @dimen/activity_horizontal_margin"
   android:paddingRight=" @dimen/activity_horizontal_margin"
   android:paddingTop=" @dimen/activity_vertical_margin"
   tools:context=".MainActivity"
  
   >

   <ListView
     android:id="@+id/listview"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:background="#44aa77"
    
     />
      
</LinearLayout>


Now create a new xml file the res/animator folder. The file name should be slide.xml. In this file, there are two animations (fade-in, and slide-in) are played in a set sequentially. The fade-in makes changes to the alpha property of the ListView. The slide-in animation changes the y property of the ListView. All durations of the animations are set to 1000ms.  

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android= "http://schemas. android.com/apk/res/android" android:ordering="sequentially">
   <!-- fade in -->
   <objectAnimator
     android:propertyName="alpha"
     android:duration="1000"
     android:valueFrom="0.0"
     android:valueTo="1.0"
     android:valueType="floatType"
     />  

   <!-- slide in-->
<objectAnimator
   android:propertyName="y"
     android:duration="1000"
     android:interpolator=" @android:anim/accelerate_interpolator"
     android:valueFrom="-300"
     android:valueTo="150"
     android:valueType="intType"
     />


  
</set>

To apply the set animations defined in the slide. xml file to the ListView, in onStart method of the MainActivity class, you need to write code to create a reference to the ListView, create an array of string items, create an instance of ArrayAdapter to wrap the array of items, set the adapter instance to the ListView, inflate the animation xml file, set target object to the reference of the ListView, and start the animation.

  package com.example.andtip;
import android.animation.AnimatorSet;
import android.animation.AnimatorInflater;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ListView;
public class MainActivity extends Activity {
   AnimatorSet set;
   ImageView imgView;
   int imgResources[]={R.drawable.angkorwat, R.drawable.angkorwat1};
   int index=0;
   protected void onCreate(Bundle savedInstanceState) {
   //TODO Auto-generated method stub
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
     }
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
       getMenuInflater().inflate(R.menu.main, menu);
           return true;
   }
   public void onStart(){
   super.onStart();  
   ListView listview=(ListView)findViewById(R.id.listview);
   String items[]={"Facebook","Google Plus","Twitter"};
   ArrayAdapter<String> aa=new ArrayAdapter<String>(this,R.layout.list_item, R.id.title, items);
   listview.setAdapter(aa);    
set = (AnimatorSet) AnimatorInflater.loadAnimator(this,R.animator.slide);
     set.setTarget(listview);
   set.start();
    
   }
  
}





Android-listview-slide-animation

Posted by: Dara | post date: 07-07-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.