Tuesday, 25 June 2013

Slide Show in Android



Step:- 1 Create Activity in your project
public class MainActivity extends Activity {
 ImageView  slide; //image view form layout
 Button playslide; //on click start play slide show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

        playslide = (Button)findViewById(R.id.playslide);
playslide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
   //first create array of all resource 
final int[] image = new int[]{R.drawable.audio_pause,R.drawable.audio_play,R.drawable.audio_replay,R.drawable.audio_seek,R.drawable.audio_stop};
 //create handel for update the User interface 
         final Handler handler = new Handler();
// create those can set the image to image view
final Runnable runnable = new Runnable() {@Override public void run() {slide.setImageResource(image[currentIndex% image.length]);currentIndex++;}};
//Create timer class for refresh image after some time iterval      
Timer task = new Timer();
         task.scheduleAtFixedRate(new TimerTask() {@Override public void run() {handler.post(runnable);}}, 0,4000);
}
});
}
}
Step: 2 create layout to display the image

<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"
    >

 <ImageView
       android:id="@+id/slide"
       android:layout_below="@+id/playslide"
       android:layout_height="wrap_content"
       android:layout_width="wrap_content"
       android:paddingTop="100dp"
       android:src="@drawable/ic_launcher"
       android:layout_centerInParent="true"
       />
</RelativeLayout>



No comments:

Post a Comment