using android animation-list

Solved my own problem, You cannot start animations in the oncreate. It has to be in an onclick listener or inside a runnable.


I think the most elegant and versatile option is to extend from the ImageView class:

public class Loader extends ImageView {

    public Loader(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public Loader(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public Loader(Context context) {
        super(context);
        init();
    }

    private void init() {
        setBackgroundResource(R.drawable.loader);
        final AnimationDrawable frameAnimation = (AnimationDrawable) getBackground();
        post(new Runnable(){
            public void run(){
                 frameAnimation.start();
             }
        });
    }
}

The loader.xml located in the drawable folder:

<?xml version="1.0" encoding="utf-8"?>
    <animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/loader_1" android:duration="50" />
    <item android:drawable="@drawable/loader_2" android:duration="50" />
    <item android:drawable="@drawable/loader_3" android:duration="50" />
    <item android:drawable="@drawable/loader_4" android:duration="50" />
    .....
</animation-list>

Now include in your views something as simple as this:

<com.yourpackage.Loader
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />