Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having trouble animating listview layout

I want the items in the listview to slide in one at a time from left to right. I have the following in res/anim/slide_right.xml

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

<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator">
    <translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="150" />
</set>

And in my onCreate method in my ListActivity I have:

LayoutAnimationController controller = AnimationUtils.loadLayoutAnimation(this, R.anim.slide_right);
mList.setLayoutAnimation(controller);

When I run it, I get a RuntimeException caused by Unknown layout animation name: set

What am I doing wrong?

like image 251
Christopher Perry Avatar asked Aug 10 '10 03:08

Christopher Perry


2 Answers

I had the same problem and I seem to have solved it. I think this problem is similar to this one: http://groups.google.com/group/android-developers/browse_thread/thread/2266e171b9b0cf17

I have posted my answer over there over here as well (with some edits). You will have to define a second XML file that has a layoutAnimation element in it:

"If you want to apply an animation using android:layoutAnimation (or using loadLayoutAnimation), it seems that you have to define an extra XML file that refers to your animation (scale, set, translate, etc...). You can find an example here: http://developerlife.com/tutorials/?p=343.

For example, your second xml file would look like this (let's call it example.xml):

<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android" 
    android:delay="10%"
    android:animation="@anim/slide_right"
/>

You can then refer to this animation in your layout file:

android:layoutAnimation="@anim/example"

(or in your code using loadLayoutAnimation)

"

Hope this helps.

Ciao!

like image 99
a2ronus Avatar answered Sep 19 '22 13:09

a2ronus


I am not sure if you have already found the soulution but let me tell you the way I found the solution.

Make another Android xml in ur anim folder. Let it be list_layout_controller.xml as below

<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
        android:delay="20%"
        android:animationOrder="normal"
        android:animation="@anim/slide_right">
</layoutAnimation>

Now, set list_layout_controller.xml (using @anim notation) as the animation as below and run:

LayoutAnimationController controller = AnimationUtils.loadLayoutAnimation(this, R.anim.list_layout_controller); 
mList.setLayoutAnimation(controller); 
like image 23
user403241 Avatar answered Sep 21 '22 13:09

user403241