title: Slideshow with multiple animation and Viewpager images
body: <p>I have <code>listactivity</code> app forming many rows , one row is images Slideshow , when you click
  the row open activity showing <code>ImageView</code> , also there is optionmenu of one item which is( slideshow animation setting  ), when you click it , it open checkbox preference 
  animations screen with multiple checkboxes each one apply different animation to images
  slideshow ,where user determine either to slide images with many animations available by 
  check its checkbox animation name or when uncheck all the checkboxs so the slideshow activity
  must show images in viewpager pattern .</p>
  
  <p><code>android:defaultValue="true"</code> for first animation which is fade_in animation.
  BUT: when you open slideshow activity its open the images in imagepager pattern ,and <strong>ignoring</strong> <code>android:defaultValue="true"</code> for fade_in checkbox,</p>
  
  <p>then after go to preference screen to choose another animation then back to slideshow
  activity , it doesn't apply the new animation , i have to press back button many times
  till finish all images scrolled in pager then it apply the next animation ,
  and sometimes it stuck on image pager and freeze's ,the normal behavior is applying 
  the next animation once press back button which return to slideshow .</p>
  
  <p>another thing when i was in image <code>viewpager</code> pattern and scroll it , 
  it scroll a few number of images then back to first image then i scroll images again 
  and suddenly it back to first image one and so on .</p>
  
  <p><strong>whole project can be downloaded from</strong>  <a href="http://www.mediafire.com/download/bcp58397lmub376/Slideshow.rar" rel="nofollow">here</a></p>
  
  <p>any help will be appreciated.</p>
  
  <p><strong>SlideShow.java</strong>  </p>
  
  <pre><code>public class SlideShow extends Activity {
  
  public int currentimageindex=0;
  Timer timer;
  TimerTask task;
  ImageView slidingimage;
  
    private int[] IMAGE_IDS = {
          R.drawable.day_one_1, R.drawable.day_one_2, R.drawable.day_one_3,
          R.drawable.day_one_4, R.drawable.day_one_5, R.drawable.day_one_6,
          R.drawable.day_one_7, R.drawable.day_one_8, R.drawable.day_one_9,
          R.drawable.day_one_10, R.drawable.day_one_11, R.drawable.day_one_12,
          R.drawable.day_one_13, R.drawable.day_one_14, R.drawable.day_one_15,
          R.drawable.day_one_16,R.drawable.day_one_17,R.drawable.day_one_18, 
          R.drawable.day_one_19,R.drawable.day_one_20  
          };
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.slide);
      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 = 8000; // repeat every 4 sec.
  
      Timer timer = new Timer();
  
      timer.scheduleAtFixedRate(new TimerTask() {
  
      public void run() {
  
           mHandler.post(mUpdateResults);
      }
  
      }, delay, period);             
  
  }      
  
  private void AnimateandSlideShow() {
  
      SharedPreferences getPrefs = PreferenceManager
              .getDefaultSharedPreferences(getBaseContext());
  
      boolean animation = getPrefs.getBoolean("animation", true);             
      boolean animation_one = getPrefs.getBoolean("animation_one", false);
      boolean animation_two = getPrefs.getBoolean("animation_two", false);    
      boolean animation_three = getPrefs.getBoolean("animation_three", false);
      boolean animation_four = getPrefs.getBoolean("animation_four", false);
      boolean animation_five = getPrefs.getBoolean("animation_five", false);              
  
   if (animation == true) {   
      slidingimage = (ImageView)findViewById(R.id.ImageView_slide);
      slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);       
      currentimageindex++;
      Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.custom_anim);               
        slidingimage.startAnimation(rotateimage);   
  
  }else if(animation_one == true) {
      slidingimage = (ImageView)findViewById(R.id.ImageView_slide);
      slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);       
      currentimageindex++;        
      Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.fade_in);        
        slidingimage.startAnimation(rotateimage);   
  
  }else if (animation_two == true) {
      slidingimage = (ImageView)findViewById(R.id.ImageView_slide);
      slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);       
      currentimageindex++;
      Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.fade_out);       
        slidingimage.startAnimation(rotateimage);  
  
  }else if (animation_three == true) {
      slidingimage = (ImageView)findViewById(R.id.ImageView_slide);
      slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);       
      currentimageindex++;
      Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.bounce);       
        slidingimage.startAnimation(rotateimage);  
  
  }else if(animation_four == true) {
      slidingimage = (ImageView)findViewById(R.id.ImageView_slide);
      slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);       
      currentimageindex++;        
      Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.fade_in_2);        
        slidingimage.startAnimation(rotateimage);   
  
  }else if (animation_five == true) {
      slidingimage = (ImageView)findViewById(R.id.ImageView_slide);
      slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);       
      currentimageindex++;
      Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.flip);       
        slidingimage.startAnimation(rotateimage);  
  
  
  }else if(animation == false &amp;&amp; animation_one == false &amp;&amp; animation_two == false){
      Intent intent = new Intent(SlideShow.this, ImagePager.class);                                     
      startActivity(intent);
  
  }
  }
  @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;
      }
  
     @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      // TODO Auto-generated method stub
      switch (item.getItemId()) {
  
      case R.id.action_settings:
          Intent p = new Intent("com.test.test.SETTING");
          startActivity(p);
      break;
      }
  
  return false; 
  }
     }
  </code></pre>
  
  <p><strong>ImagePager.java</strong></p>
  
  <pre><code>  public class ImagePager extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.pager);
  
      ImagePagerAdapter adapter = new ImagePagerAdapter(this, imageArra);
      ViewPager myPager = (ViewPager) findViewById(R.id.myimagepager);
      myPager.setAdapter(adapter);
      myPager.setCurrentItem(0);
  }
  
       private int imageArra[] = { R.drawable.day_one_1, R.drawable.day_one_2, R.drawable.day_one_3,
          R.drawable.day_one_4, R.drawable.day_one_5, R.drawable.day_one_6,
          R.drawable.day_one_7, R.drawable.day_one_8, R.drawable.day_one_9,
          R.drawable.day_one_10, R.drawable.day_one_11, R.drawable.day_one_12,
          R.drawable.day_one_13, R.drawable.day_one_14, R.drawable.day_one_15,
          R.drawable.day_one_16,R.drawable.day_one_17,R.drawable.day_one_18, 
          R.drawable.day_one_19,R.drawable.day_one_20  
  
      };
  
  public class ImagePagerAdapter extends PagerAdapter {   
      Activity activity;
      int[] imageArray;
  
      public ImagePagerAdapter(Activity act, int[] imgArra) {
          imageArray = imgArra;
          activity = act;   
          }
  
      public int getCount() {
          return imageArray.length;   
          }
  
      public Object instantiateItem(View collection, int position) {
          LayoutInflater inflater = (LayoutInflater)collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          View layout = inflater.inflate(R.layout.custom_pager, null);   
  
          ImageView im=(ImageView) layout.findViewById(R.id.pager_imageView);
  
          im.setImageResource(imageArray[position]);
  
          ((ViewPager) collection).addView(layout, 0);
             return layout;   
             }
  
      @Override
      public void destroyItem(View arg0, int arg1, Object arg2) {
          ((ViewPager) arg0).removeView((View) arg2);   
          }
  
      @Override
      public boolean isViewFromObject(View arg0, Object arg1) {
          return arg0 == ((View) arg1);   
          }
  
      @Override
      public Parcelable saveState() {
          return null; 
          }
      } 
  @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;
      }
  
     @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      // TODO Auto-generated method stub
      switch (item.getItemId()) {
  
      case R.id.action_settings:
          Intent p = new Intent("com.test.test.SETTING");
          startActivity(p);
      break;
      }
  
  return false;
  }
     } 
  </code></pre>