The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.


Widget Catalog: ViewFlipper

A ViewFlipper behaves a bit like a FrameLayout that is set up such that only one child can be visible at a time. You can control which of those children is visible, either by index or via showNext()/showPrevious() methods to rotate between them.

You can also set up animated effects to control how a child leaves and the next one enters, such as applying a sliding effect.

And, you can set up ViewFlipper to automatically flip between children on a specified period, without further developer involvement. This, coupled with the animation, can be used for news tickers, ad banner rotations, or the like where light animations (e.g., fade out and fade in) can be used positively.

Key Usage Tips

ViewFlipper can have as many children as needed (within memory constraints), though you will want at least two for it to be meaningful.

By default, the transition between children is an immediate “smash cut” — the old one vanishes and the new one appears instantaneously. You can call setInAnimation() and/or setOutAnimation() to supply an Animation object or resource to use for the transitions instead.

By default, the ViewFlipper will show its first child and stay there. You can manually flip children via showNext(), showPrevious(), and setDisplayedChild(), the latter of which taking a position index of which child to display. You can also have automatic flipping, by one of two means:

  1. In your layout, android:flipInterval will set up the amount of time to display each child before moving to the next, and android:autoStart will indicate if the automated flipping should begin immediately or not
  2. In Java, setFlipInterval() serves the same role as android:flipInterval, and you can control when flipping is enabled via startFlipping() and stopFlipping()

A Sample Usage

The preview of this section took that left turn at Albuquerque.

Visual Representation

The preview of this section was accidentally identified as an Android 'tasty treat' by the Cookie Monster.