Android Image SlideShow Tutorial

 

Android image slideshow :

Introduction

Android Image slideshow is a common aspect these days in every android application it might be a website, app or any other case.

Image slideshow has been a key role in show casting the main use of the app by showing relevant images with a specific time period.

Image slide show may be automatic or based on user click sometimes.

 

Today we will go through image slideshow tutorial in android app in this blog, this tutorial building an image slideshow will clearly explain you how a slide show is made using file handlers and timers in android.

 

OK now let’s start android image auto slideshow tutorial by designing our screen first. 

 

activity_main.xml :

Here we don’t require any thing special but just a image view to make slide show and a text view.

 

<ImageView
    android:id="@+id/imageview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

 

Android Image Slideshow Full Code :

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#ffffff"
    android:gravity="center">


    <ImageView
        android:id="@+id/imageview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Android Image Slide Show"
        android:textColor="#000000"
        android:gravity="center"
        android:layout_marginTop="40dp"
        style="@android:style/TextAppearance.DeviceDefault.Widget.PopupMenu.Large"/>

</LinearLayout>

fadein.xml file

This is the xml file which helps us to made kinf of style for our images while animating them i.e., android slideshow settings kind of file. Here you can adjust according to your requirements.

 

Note: You have to make a folder inside res folder and then name it as anim then path is

 

R.anim.fadein

 

not only “anim” you can give any name to the folder but make change in th path accordingly

 

<?xml version="1.0" encoding="utf-8" ?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <alpha
        android:fromAlpha="0.0"
        android:toAlpha="1.0"
        android:duration="1000">
    </alpha>
</set>

 

 

MainActivity.java :

 

Firstly resources i.e., images are to be selected and added to a array which is of type int.

 

int[] Pics = {R.drawable.pic1, R.drawable.pic2, R.drawable.pic3, R.drawable.pic4};

 

Then here comes our Handler

 

final Handler mHandler = new Handler();

 

initialize timer

 

Timer timer;

int delay = 1000;

int period = 1000;

timer = new Timer();

 

Setting images from array to the image view

 

image.setImageResource(Pics[index%Pics.length]);

 

Animate image with fade in style

 

Animation fadein = AnimationUtils.loadAnimation(MainActivity.this, R.anim.fadein);

image.startAnimation(fadein);

 

and also don’t forget to increment index

 

index++;

 

here come our final code android code for image slideshow  

 

package com.androidcoding.abhishek.imageslider;

import android.app.Activity;
import android.os.Bundle;
import android.view.*;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.os.Handler;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends Activity {

    int index=0;
    ImageView image;
    Timer timer;

    int[] Pics = {R.drawable.pic1, R.drawable.pic2, R.drawable.pic3, R.drawable.pic4};


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Handler mHandler = new Handler();

        image = (ImageView)findViewById(R.id.imageview);


        final Runnable start = new Runnable() {
            public void run() {


                image.setImageResource(Pics[index%Pics.length]);

                index++;

                Animation fadein = AnimationUtils.loadAnimation(MainActivity.this, R.anim.fadein);

                image.startAnimation(fadein);

            }
        };

        int delay = 1000;

        int period = 1000;

        timer = new Timer();

        timer.scheduleAtFixedRate(new TimerTask() {

            public void run() {

                mHandler.post(start);

            }

        }, delay, period);

    }


}

 

Android image slideshow output :

The screen below depicts image slideshow

android image slideshow

android image slideshow

 

If you have any query’s in this tutorial on image slideshow do let us know in the comment section below.If you like this tutorial do like and share us for more interesting updates.

Show Buttons
Hide Buttons
Read previous post:
Android Tutorial on Popup Window

Android popup box : Introduction : Android popup box, Generally we see android popup box appearing in the apps showing...

Close