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. */
   public void onCreate(Bundle savedInstanceState) {

        myTextView= (TextView)findViewById(;

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

   public void onWindowFocusChanged (boolean hasFocus) {
      if (hasFocus)

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=""
       android:fromAlpha="0.0" android:toAlpha="1.0"
       android:duration="1500" />