Even if you have a phone with excellent screen resolution, the
physical screen size is still rarely over 3″x5″, since most people want phones that
can fit in a pocket, purse, pouch, or poncho.
This means your Android activities can only display so much stuff
at one time without individual widgets or text getting too small to be easily read.
There are any number of ways to handle this:
You could break the one activity into several…but this can get cumbersome
You can use ScrollView to allow users to scroll
through a longer activity…but this may or may not be easy for the user depending
on the device (e.g., a haptic interface using a swipe for scrolling assumes
there’s a place clear in the UI for them to actually swipe without accidentally
toggling a button or something)
You can use TabView, as described in the
previous post…but you are locked into a particular presentation pattern
(e.g., tabs)
You can use ViewFlipper and work out a UI pattern that’s right
for you
Not surprisingly, today, we’re going to look at the latter option.
ViewFlipper inherits from FrameLayout,
just like we used to describe the innards of a TabView in that
previous post. However, initially, it just shows the first child view. It is
up to you to arrange for the views to flip, either manually by user interaction,
or automatically via a timer.
For example, here is a layout for a simple activity using a
Button and a ViewFlipper:
Notice that the layout defines three child views for the
ViewFlipper, each a TextView with a simple message. Of
course, you could have very complicated child views, if you so chose.
To manually flip the views, we need to hook into the Button
and flip them ourselves when the button is clicked:
public class FlipperDemo extends Activity {
ViewFlipper flipper;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
flipper=(ViewFlipper)findViewById(R.id.details);
Button btn=(Button)findViewById(R.id.flip_me);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
flipper.showNext();
}
});
}
}
This is just a matter of calling showNext() on the ViewFlipper,
like you can on any ViewAnimator class.
The result is a trivial activity: click the button, and the next
TextView in sequence is displayed, wrapping around to the first after
viewing the last:
This, of course, could be handled more simply by having a single
TextView and changing the text and color on each click. However, you
can imagine that the ViewFlipper contents could be much more complicated,
like the contents you might put into a TabView.
As with the TabView in the
previous post, sometimes, your ViewFlipper contents may not be
known at compile time. As with TabView, though, you can add new contents
on the fly with ease.
For example, let’s look at another sample activity, using this layout:
Notice that the ViewFlipper has no contents
at compile time. Also note that there is no Button for flipping between
the contents — more on this in a moment.
For the ViewFlipper contents, we will create large
Button widgets, each containing one of the random words used in many
an Android tutorial. And, we will set up the ViewFlipper to automatically
rotate between the Button widgets, using an animation for transition:
After getting our ViewFlipper widget from the layout,
we first set up the “in” and “out” animations. In Android terms, an animation is
a description of how a widget leaves (”out”) or enters (”in”) the viewable area.
Animations are a complex beast, worthy of a blog post or two in their own right.
For now, realize that animations are resources, stored in res/anim/
in your project. For this tutorial, we are using a pair of animations supplied by
the SDK samples, available under the Apache 2.0 license. As their names suggest,
widgets are “pushed” to the left, either to enter or leave the viewable area.
After iterating over the funky words, turning each into a
Button, and adding the Button as a child of the
ViewFlipper, we set up the flipper to automatically flip between children
(flipper.setFlipInterval(2000);) and to start flipping (flipper.startFlipping();).
The result is an endless series of buttons, each appearing, then
sliding out to the left after 1.5 seconds, being replaced by the next button in
sequence, wrapping around to the first after the last has been shown:
In this case, the above screenshot shows the ViewFlipper in mid-transition,
animating away from one button to the next.
The auto-flipping ViewFlipper is useful for status
panels or other situations where you have a lot of information to display, but not
much room. The key is that, since it automatically flips between views, expecting
users to interact with individual views is dicey — the view might switch away part-way
through their interaction.
How to display a JPG...
Is that posible to zoom in and drag p...
Making a custom Andr...
Cool - I will use it carefully i promise
How to display a JPG...
problem in image loading - i have exe...
Introducing Calculon...
Thank You for your contribution - Hi,...
Debug a Native Appli...
Never mind, I somehow overlooked the ...