Quick Tip: Android ViewFlipper Jump/Move/Switch To a Certain Child View

At times you might want to jump the user to a specific View inside a ViewFlipper on the click of some other View or some such scenario. So let’s say we have a ViewFlipper in our layout resource like this:

<ViewFlipper
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/viewFlipper"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/first">

        <!-- more views -->

    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/second">

        <!-- more views -->

    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/third">

        <!-- more views -->
        
    </LinearLayout>
</ViewFlipper>

When the Activity loads (is created) it instantiates the ViewFlipper and shows up the first child View (LinearLayout with the ID of first). If we want to programatically move/switch to a particular view position which is not the next one, in this case the third one then all we have to do is this:

What's the one thing every developer wants? More screens! Enhance your coding experience with an external monitor to increase screen real estate.

ViewFlipper viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper);
// 2 is the index for the 3rd child
viewFlipper.setDisplayedChild(2);
// or when you don't know the index but the ID of the view
viewFlipper.setDisplayedChild( viewFlipper.indexOfChild(findViewById(R.id.third)) );

Hope that helps!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Author: Rishabh

Rishabh is a full stack web and mobile developer from India. Follow me on Twitter.

One thought on “Quick Tip: Android ViewFlipper Jump/Move/Switch To a Certain Child View”

  1. Rishabh,

    Thanks for the ViewFlipper tutorial. I have a problem though. Can you tell me where I went wrong with your code? I am using Android Studio 1.1.0:

    I am getting a error message at:

    mViewFlipper.showNext();
    mViewFlipper.showPrevious();

    and

    mGestureDetector.onTouchEvent(event);

    Code follows:

    import android.app.Activity;
    import android.os.Bundle;
    import android.support.v7.app.ActionBarActivity;
    import android.view.GestureDetector;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.MotionEvent;
    import android.widget.ImageView;
    import android.widget.ViewFlipper;
    import android.widget.ViewSwitcher;

    public class gesture extends ActionBarActivity {

    public class GestureActivity extends Activity {

    private ViewFlipper mViewFlipper;
    private GestureDetector mGestureDetector;

    int[] resources = {
    R.drawable.image1,
    R.drawable.image2,
    R.drawable.image3,
    R.drawable.image4

    };

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gesture);

    // Get the ViewFlipper//
    mViewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper);

    // Add all the images to the ViewFlipper

    for (int i = 0; i e2.getX()) {
    mViewFlipper.showNext();
    }

    // Swipe right (previous)
    if (e1.getX() Compilation failed; see the compiler error output for details.

Leave a Reply

Your email address will not be published. Required fields are marked *