Start animation in onCreate or onResume on Android?

June 8, 2011

Maybe you’d like to perform a nice subtle animation such as fading some text in as soon as your activity launches? If you try to start this animation in onCreate() or onResume() you’ll be sorely dissapointed.

One possible solution is to set a timer in one of these methods so that the animation will spawn after a fixed interval.  This approach is problematic because if the delay is too short the animation won’t start (as before) and if it’s too long then the user experience will drop. Furthermore, such a delay might not behave quite the same on a G1 compared to a spanking new Samsung Galaxy S II for example.

A better way is to start the animation in the onWindowFocussedChanged method which is called when the current window of the activity gains or loses focus and gives the best indicator whether the activity is visible to the user. You can use the *hasFocus* parameter to distinguish between gaining or losing focus. Example below:

   private TextView myTextView;

   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        myTextView= (TextView)findViewById(R.id.my_textview);

        anim = AnimationUtils.loadAnimation(this, R.anim.fade_in);
    }

   @Override
   public void onWindowFocusChanged (boolean hasFocus) {
      super.onWindowFocusChanged(hasFocus);
      if (hasFocus)
         myTextView.startAnimation(anim);
   }

The animation itself is defined in an xml file in res/anim/fade_in.xml:

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
       android:interpolator="@android:anim/accelerate_interpolator"
       android:fromAlpha="0.0" android:toAlpha="1.0"
       android:duration="1500" />
Advertisements

5 Responses to “Start animation in onCreate or onResume on Android?”

  1. Daniel Says:

    Thanks, this was very useful. But, what about in a Tab situation? When I change the tab, the onWindowFocusChanged it’s not executed, and I have the weird issue with the onResume, that does not start the animation the first time but it does when I return to the activity.
    Do you have a workaround for this situation?

    Thanks!


  2. For individual views, onAttachedToWindow() could be easily used as well. Just post a quick runnable to the View and call invalidate(). Works like a charm.


  3. […] cant start animation from onCreate method. There is not view yet and activity is not visible. Read this and change you code a little and it will work. I hope so […]

  4. sirk214@gmx.de Says:

    thanks for sharing! i was just starting to wonder why the f#*# 😉

  5. alaxid Says:

    You should really take a look at addOnGlobalLayoutListener. It will fire sooner than onWindowFocusChanged.

    myView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
    doYourStuff.
    }
    });

    Try to put both solutions and have a logger with the current clock time.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: