1.03.2012

Using ViewFlipper in Android App

Suppose you want to display a news bar in your activity. this news bar displays a single news item at a time then flips and shows next item and so on, then your choice would be Android's ViewFlipper.

ViewFlipper inherits from frame layout, so it displays a single view at a time.
consider this layout:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.    android:orientation="vertical"
  4.    android:layout_width="fill_parent"
  5.    android:layout_height="fill_parent"
  6.    >
  7. <TextView
  8.    android:layout_width="fill_parent"
  9.    android:layout_height="wrap_content"
  10.    android:text="@string/hello"/>
  11.     <Button
  12.    android:layout_width="wrap_content"
  13.    android:layout_height="wrap_content"
  14.    android:text="Flip"
  15.    android:id="@+id/btn"
  16.    android:onClick="ClickHandler"/>
  17.     <ViewFlipper
  18.    android:layout_width="fill_parent"
  19.    android:layout_height="fill_parent"
  20.    android:id="@+id/flip">
  21.     <TextView
  22.    android:layout_width="fill_parent"
  23.    android:layout_height="wrap_content"
  24.    android:text="Item1"/>
  25.     <TextView
  26.    android:layout_width="fill_parent"
  27.    android:layout_height="wrap_content"
  28.    android:text="Item2"/>
  29.     <TextView
  30.    android:layout_width="fill_parent"
  31.    android:layout_height="wrap_content"
  32.    android:text="Item3"/>
  33.     </ViewFlipper>
  34. </LinearLayout>


Just a ViewFlipper container that contains three text views

Now we want to flip the views when the button is clicked.

  1. public void onCreate(Bundle savedInstanceState) {
  2.     super.onCreate(savedInstanceState);
  3.     setContentView(R.layout.main);
  4.     btn=(Button)findViewById(R.id.btn);
  5.     flip=(ViewFlipper)findViewById(R.id.flip);
  6. }
  7. public void ClickHandler(View v)
  8. {
  9.     flip.showNext();
  10. }


If we want to flip in reverese direction we could use flip.showPrevious() instead. If you want to flip to a specific view do the following: flip.setDisplayedChild(indexOfView) replace indexOfView with the integer representing the index of the view you want to switch to.

We can add animations to the child views when they appear or disappear:
  1. public void onCreate(Bundle savedInstanceState) {
  2.     super.onCreate(savedInstanceState);
  3.     setContentView(R.layout.main);
  4.     btn=(Button)findViewById(R.id.btn);
  5.     flip=(ViewFlipper)findViewById(R.id.flip);
  6.     //when a view is displayed
  7.     flip.setInAnimation(this,android.R.anim.fade_in);
  8.     //when a view disappears
  9.     flip.setOutAnimation(this, android.R.anim.fade_out);
  10. }

We can also set the ViewFlipper to flip views automatically when the button is clicked:

  1. public void ClickHandler(View v)
  2. {
  3.     //specify flipping interval
  4.     flip.setFlipInterval(1000);
  5.     flip.startFlipping();
  6. }

We can stop the flipping by calling flip.stopFlipping(); method or we can set the flipper to flip autommatically when the activity starts by changing our code.

  1. public void onCreate(Bundle savedInstanceState) {
  2.     super.onCreate(savedInstanceState);
  3.     setContentView(R.layout.main);
  4.     btn=(Button)findViewById(R.id.btn);
  5.     flip=(ViewFlipper)findViewById(R.id.flip);
  6.     flip.setInAnimation(this,android.R.anim.fade_in);
  7.     flip.setOutAnimation(this, android.R.anim.fade_out);
  8.     flip.setFlipInterval(1000);
  9.     flip.setAutoStart(true);    
  10. }

More about this control you can read by this url: http://developer.android.com/reference/android/widget/ViewFlipper.html

No comments:

Post a Comment