Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move to a certain view using ViewFlipper?

Tags:

android

I would like to switch to certain views in a ViewFlipper. Currently, I have 6 children inside the ViewFlipper. And I am having some buttons for navigation. It is very much similar to "News and weather" application.

like image 566
Ye Myat Min Avatar asked Jul 04 '10 11:07

Ye Myat Min


People also ask

What is view Flipper?

ViewFlipper in android is an extension of ViewAnimator class, which animates between two or more views that have been added to it. Only one child is shown at a time and the user can flip to view the other child views.

What is Flipper in android?

Flipper is a platform for debugging iOS, Android and React Native apps. Visualize, inspect, and control your apps from a simple desktop interface. Use Flipper as is or extend it using the plugin API.


2 Answers

See this simple complete example of android.widget.ViewFlipper. With it you can create different layout from xml and then switch among them with simple method like this:

   ViewFlipper viewFlipper = (ViewFlipper) findViewById(R.id.myViewFlipper);     // you can switch between next and previous layout and display it    viewFlipper.showNext();    viewFlipper.showPrevious();    // or you can switch selecting the layout that you want to display   viewFlipper.setDisplayedChild(1);   viewFlipper.setDisplayedChild(viewFlipper.indexOfChild(findViewById(R.id.secondLayout) 

Xml example with tree layouts:

     <ViewFlipper             android:id="@+id/myViewFlipper"             android:layout_width="match_parent"             android:layout_height="match_parent" >              <LinearLayout                 android:id="@+id/firstLayout"                 android:layout_width="match_parent"                 android:layout_height="match_parent"                 android:orientation="vertical" >                [...]             </LinearLayout>              <LinearLayout                 android:id="@+id/secondLayout"                 android:layout_width="match_parent"                 android:layout_height="match_parent"                 android:orientation="vertical" >                [...]             </LinearLayout>              <LinearLayout                 android:id="@+id/thirdLayout"                 android:layout_width="match_parent"                 android:layout_height="match_parent"                 android:orientation="vertical" >               [...]             </LinearLayout>       </ViewFlipper> 
like image 36
lory105 Avatar answered Sep 24 '22 08:09

lory105


Call setDisplayedChild(), passing the 0-based index of the child View you want to display.

like image 131
CommonsWare Avatar answered Sep 25 '22 08:09

CommonsWare