Christopher Perry Christopher Perry - 4 months ago 8
Android Question

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?

Answer

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!

Comments