How to start Animation immediately after onCreate?

You can use the post method of any View on the activity. It should probably look like this:

View anyView = findViewById(R.id.anyView);
anyView.post(new Runnable()
{
    @Override
    public void run()
    {
        // Your code goes here
    }
});

Have a flag set in onAttachedToWindow() and then in onWindowFocusChanged() check it and start the animation.

@Override
void onWindowFocusChanged(boolean hasFocus) {
    if (hasFocus & mbFlag) {
        // start animation.
    }
}

Update

Simply extend the ImageView class and override onFocusChange method. Then in your activity set the focus to it by calling animImg.requestFocus(). The animation should start when it gets focused. Make sure your imageview is focusable.

If this does not work, you may want to override the onAttachedToWindow() method also. Set a flag in there and check before starting the animation.

@Override
void onFocusChange(boolean hasFocus) {
    if (hasFocus) {
        // start animation.
    }
}

It's already written in the tutorial:

It's important to note that the start() method called on the AnimationDrawable cannot be called during the onCreate() method of your Activity, because the AnimationDrawable is not yet fully attached to the window.

If you want to play the animation immediately, without requiring interaction, then you might want to call it from the onWindowFocusChanged() method in your Activity, which will get called when Android brings your window into focus.

So move your call to start in one of those two places, depending on your wish. Based on your comment, move your call to start inside onWindowsFocusChanged().

EDIT So this is "How to do it":

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    if(hasFocus){
        textView.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,
            android.R.anim.slide_in_left|android.R.anim.fade_in));
    }   
}

The points to pay attention to are:

  • do not forget to write the if/else case to check the focus
  • and delete the auto-generated "super.onWindowFocusChanged(hasFocus);"