How to Perform Slideshow of Images using ViewFlipper Control in Android

In this tutorial how to perform slideshow of Images using ViewFlipper Control in Android is shown.

Points to Remember:

  • To specify the time interval between images in the slideshow write android:flipInterval=” ” property inside the <ViewFlipper> tag.
  • Whatever time you specify here is in milliseconds For eg. android:flipInterval=”1000″ will flip images in 1 second.

Code:

res/values/strings.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<resources>

<string name=”app_name”>Demo</string>
<string name=”action_settings”>Settings</string>
<string-array name=”image_list”>
<item>Desert</item>
<item>Koala</item>
<item>JellyFish</item>
<item>LightHouse</item>
<item>Penguins</item>
</string-array>
<string name=”start”>Click Here to Start Slideshow</string>
<string name=”stop”>Click Here to Stop Slideshow</string>
<color name=”black”>#000000</color>

</resources>

activity_main.xml

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:paddingBottom=”@dimen/activity_vertical_margin”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
tools:context=”.MainActivity”
android:background=”@color/black”>

<ViewFlipper
android:id=”@+id/viewFlipper1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”160dp” >

<ImageView
android:id=”@+id/imageView3″
android:layout_width=”wrap_content”
android:layout_height=”299dp”
android:src=”@drawable/koala” />

<ImageView
android:id=”@+id/imageView4″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:src=”@drawable/lighthouse” />

<ImageView
android:id=”@+id/imageView5″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:src=”@drawable/penguins” />

<ImageView
android:id=”@+id/imageView1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:src=”@drawable/desert” />

<ImageView
android:id=”@+id/imageView2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:src=”@drawable/jellyfish” />

</ViewFlipper>

<Button
android:id=”@+id/button1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”49dp”
android:text=”@string/start” />

</RelativeLayout>

MainActivity.java

package com.example.demo;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ViewFlipper;

public class MainActivity extends Activity{

int myflipping=0;
Button b;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
ViewFlipper flipper=(ViewFlipper)findViewById(R.id.viewFlipper1);
if(myflipping==0)
{
flipper.startFlipping();
myflipping=1;
b.setText(R.string.stop);
}
else
{
flipper.stopFlipping();
myflipping=0;
b.setText(R.string.start);
}
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
//Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

 

https://youtu.be/IfkcbrRZMWg

Add a Comment

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