Working with View Switcher Controls

For situations in which an activity is going to be updating the content of a View control repeatedly, the Android SDK provides a mechanism called a ViewSwitcher control. Using a ViewSwitcher is an efficient way to update content on a screen. A ViewSwitcher control has two children and handles transition from the currently visible child view to the next view to be displayed. The child View controls of a ViewSwitcher control can be generated programmatically using ViewFactory.

There are two subclasses of the ViewSwitcher class:

► TextSwitcher—A ViewSwitcher control that allows swapping between two TextView controls.

► ImageSwitcher—A ViewSwitcher control that allows swapping between two ImageView controls.

Although a ViewSwitcher control only ever has two children, it can display any number of View controls in succession. ViewFactory generates the content of the next view, such as the ImageSwitcher and TextSwitcher controls for iterating through the question images and text.


You can create a custom switcher by implementing your own subclass of the ViewSwitcher class.

Using ViewFactory to Generate ViewSwitcher Views

When you create a ViewSwitcher control, you can configure ViewFactory using the setFactory() method. ViewFactory has one required method, the makeView() method. This method must return a View of the appropriate type. For example, ViewFactory for TextSwitcher should return a properly configured TextView, whereas ViewFactory for ImageSwitcher would return ImageView.

Here is an implementation of a ViewFactory control for an ImageSwitcher control that you could use to generate each question graphic on the game play screen:

private class MyImageSwitcherFactory implements ViewSwitcher.ViewFactory { public View makeView() {

ImageView imageView = new ImageView(QuizGameActivity.this); imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); imageView .setLayoutParams(new ImageSwitcher.LayoutParams(

LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); return imageView ;

Note that the source, or contents, of the view have not been configured in the makeView() method. Instead, you can consider this a template that the ViewSwitcher control will use to display each child view.

When you create a ViewSwitcher control, you can configure its ViewFactory using the setFactory() method. For example, to set ViewFactory of the ImageSwitcher control you created for MyImageSwitcherFactory, you do the following:

ImageSwitcher questionImageSwitcher =

(ImageSwitcher) findViewById(; questionImageSwitcher.setFactory(new MyImageSwitcherFactory());

Similarly, you must create a ViewFactory to generate the TextView controls for each question on the game screen. Here is an implementation of a ViewFactory called MyTextSwitcherFactory that does just that:

private class MyTextSwitcherFactory implements ViewSwitcher.ViewFactory { public View makeView() {

TextView textView = new TextView(QuizGameActivity.this);


Resources res = getResources();

float dimension = res.getDimension(R.dimen.game_question_size);

int titleColor = res.getColor(R.color.title_color);

int shadowColor = res.getColor(R.color.title_glow);



textView.setShadowLayer(10, 5, 5, shadowColor);

return textView;

Note that, much like the MyImageSwitcherFactory implementation, the MyTextSwitcherFactory also implements the makeView() method—this time generating the appropriate TextView control with some text size, color, and gravity attributes.

Working with TextSwitcher

The TextSwitcher control enables an activity to animate between two TextView controls. You need to include a TextSwitcher control called TextSwitcher_ QuestionText in the layout of the game screen to display each trivia question to the user.

0 0

Post a comment