How to set dynamic images to ViewFlipper in android?

In your xml just add only this,

<ViewFlipper android:id="@+id/flipper" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"> 
</ViewFlipper>

need not enclose with any ImageViews.

Now do this in your coding.

Considering that you have stored your Images in a array like this,

int gallery_grid_Images[]={R.drawable.gallery_image_1,R.drawable.gallery_image_2,R.drawable.gallery_image_3,
        R.drawable.gallery_image_4,R.drawable.gallery_image_5,R.drawable.gallery_image_6,R.drawable.gallery_image_7,
        R.drawable.gallery_image_8,R.drawable.gallery_image_9,R.drawable.gallery_image_10
        };

Now in your onCreate(),

viewFlipper = (ViewFlipper) findViewById(R.id.flipper);
 for(int i=0;i<gallery_grid_Images.length;i++)
        {
        //  This will create dynamic image view and add them to ViewFlipper
            setFlipperImage(gallery_grid_Images[i]);
        }

And now add this method to your activity,

private void setFlipperImage(int res) {
    Log.i("Set Filpper Called", res+"");
    ImageView image = new ImageView(getApplicationContext());
    image.setBackgroundResource(res);
    viewFlipper.addView(image);
}

That’s it. And now using your viewFlipper.showNext(); and viewFlipper.showPrevious();
methods you can show your dynamic images.

Leave a Comment