Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make ListView animation in android

    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 3.54k
    Comment on it

    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)

  • Nice, thank you!!
    But if you scroll down before the animations finished, the items you didn't see before are already displayed. The animation is just for the items you can see without scrolling.
    How to make the animation for ALL items in the Listview?
Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: