Image slide show



main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/black">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/TextViewTopTitle"
        android:text="@string/app_logo_top"
        android:textColor="@color/logo_color"
        android:layout_gravity="center_vertical|center_horizontal"
        android:gravity="top|center"
        android:textSize="@dimen/logo_size"></TextView>
       
         <Button android:id="@+id/close"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:text="Exit"
    android:onClick="onClick" />
   
         <ImageView
                android:id="@+id/ImageView3_Left"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"
                ></ImageView>
               
              

</LinearLayout>

SliderActivity.java


package moor.android.slide;

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 SliderActivity extends Activity {
   
    public int currentimageindex=0;
//    Timer timer;
//    TimerTask task;
    ImageView slidingimage;
   
    private int[] IMAGE_IDS = {
            R.drawable.splash0, R.drawable.splash1, R.drawable.splash2,
            R.drawable.splash3
        };
   
   
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mygame);
        final Handler mHandler = new Handler();

        // Create runnable for posting
        final Runnable mUpdateResults = new Runnable() {
            public void run() {
               
                AnimateandSlideShow();
               
            }
        };
       
        int delay = 1000; // delay for 1 sec.

        int period = 15000; // repeat every 4 sec.

        Timer timer = new Timer();

        timer.scheduleAtFixedRate(new TimerTask() {

        public void run() {

             mHandler.post(mUpdateResults);

        }

        }, delay, period);
       
       
              
    }

    public void onClick(View v) {
   
        finish();
        android.os.Process.killProcess(android.os.Process.myPid());
      }
   
    /**
     * Helper method to start the animation on the splash screen
     */
    private void AnimateandSlideShow() {
       
       
        slidingimage = (ImageView)findViewById(R.id.ImageView3_Left);
           slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);
          
           currentimageindex++;
       
           Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.fade_in);
         
       
          slidingimage.startAnimation(rotateimage);
         
             
       
    }   
   
   
}


Screen shot :








Download source...

33 comments:

  1. Simplest Android Slideshow!! Thanks!

    ReplyDelete
  2. the download link is not working. The source file is corrupt. But, the code given here is fine.

    See the line -

    Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.fade_in);

    Here, what is "R.anim.fade_in"? how can i import fade-in into anim folder?

    ReplyDelete
    Replies
    1. link is working keerthi try again please...

      R.anim.fade_in is a XML file which is under anim folder in res,we can create different types of animation in this folder.

      thanks for your feedback...

      Delete
    2. try android.R.anim.fade_in instead.

      Delete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Hello Mr.Moorthy, the code is fine, but now I want to know, how images can be loaded from res/drawable folder into IMAGE_IDS array, with a reference of the file name stored in a column say COL_DATA? I mean, I have some files in drawable folder, I have the file names in the COL_DATA Column. Now how can I retrieve the path of those images into IMAGES_IDS array to make the animation?

    ReplyDelete
    Replies
    1. you can get the images from drawable folder by using R.drawable.imagename in java code.

      Delete
  5. http://www.techipost.com/2011/06/15/android-image-slider-with-auto-slide-code/

    so who is copying the code and who is original author?

    ReplyDelete
  6. getting error with "mygame"
    R.layout.mygame

    ReplyDelete
    Replies
    1. It is your name of your main activity
      Replace by your main activity name

      Delete
  7. It isn't a tutorial. You didn't explain anything. What did you think tutorial meant?

    ReplyDelete
  8. Moorthy Bhai,,,Excellent!!! Thanks alot...you have saved my day!!1

    ReplyDelete
  9. how to create the fade_in xml file..i mean can u just post that part of code would of great help

    ReplyDelete
  10. how can I change the rotation animation into the right to left sliding animation.

    ReplyDelete
    Replies
    1. There are two methods in R.anim class named
      R.anim.slide_in_left() and R.anim.slide_out_right()

      Hope you'll get your answer

      Delete
  11. How can I present my photos in a fullscreen mode (without the top bar "exit")? Can I make a 24-hour presentation?

    ReplyDelete
  12. Hey I want a code that will slide image automatically as well as manually.

    ReplyDelete
  13. I am trying to use your image slide show code in one of my android app. It is working fine, but I see a log of heap allocation/freeing messages. Googling on it, I found we get these messages when we have a bad coding style leading to heap issues.
    Do you know what is the issue and if you can suggest a better coding style?
    The log has these:

    04-24 18:51:10.041: D/dalvikvm(2377): GC_FOR_ALLOC freed 72K, 5% free 2657K/2780K, paused 45ms, total 50ms
    04-24 18:51:10.210: D/dalvikvm(2377): GC_FOR_ALLOC freed 3K, 3% free 5959K/6088K, paused 143ms, total 144ms
    04-24 18:51:13.030: D/dalvikvm(2377): GC_FOR_ALLOC freed 2K, 3% free 5960K/6088K, paused 55ms, total 56ms

    ReplyDelete
  14. This is one of the best and concise sites. How I wish every one knew about it!!

    ReplyDelete
  15. how can slide more then 2 images?

    ReplyDelete
  16. what if we want to stop/pause animation not close activity?

    ReplyDelete
  17. using more than 30 images app cannot load or cannot work....
    how can slide more than 30 images...

    ReplyDelete
  18. Paint Free is best android app for making drawings.
    http://techpave.com

    ReplyDelete
  19. Gitanjali GhangaleAugust 4, 2016 at 1:58 AM

    simple and quick understandable. Thank you :-)

    ReplyDelete
  20. thanks moorthy... it was really helpful .and worked in the first time only.... unlike other :)

    ReplyDelete
  21. thanks moorthy... it was really helpful .and worked in the first time only.... unlike other :)

    ReplyDelete
  22. Hi Moorthy, The link did not work for me to download the code. Please indicate another link.

    ReplyDelete
  23. i want to continuesly scroll my imge like as marqueeRepeat of text view.
    plz help that.

    ReplyDelete