How to move to a certain view using ViewFlipper?

Android

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

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>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionYe Myat MinView Question on Stackoverflow
Solution 1 - AndroidCommonsWareView Answer on Stackoverflow
Solution 2 - Androidlory105View Answer on Stackoverflow