Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: I've created a slideshow using ViewPager, need to make a 3 small changes but stuck

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question I've created a slideshow using ViewPager, need to make a 3 small changes but stuck

    I wanted to create a simple slideshow. I've gotten reasonably far, but now I'm stuck, I'm sure my issues sound very simple to you guys, hence why I'm here looking for help :-) I have made an effort, source code below.


    Changes I'm stuck on

    1. I want the images of the slideshow to fill the screen
    2. I also don't want the user to have to drag the image across, I just want him to tap the image and it goes to the next one.
    3. When the slideshow ends (reaches last item, a toast message tells user its finished)

    I have tried to do the above, but like I said. I'm abit new at all this

    ImagePager:

        public class ImagePager extends Activity {
     
        	@Override
        	public void onCreate(Bundle savedInstanceState) {
        		super.onCreate(savedInstanceState);
        		setContentView(R.layout.activity_main);
        		ImagePagerAdapter adapter = new ImagePagerAdapter(this, imageArra );
        		ViewPager myPager = (ViewPager) findViewById(R.id.myimagepager);
        		myPager.setAdapter(adapter);
        		myPager.setCurrentItem(0);
        		}
     
     
        	private int imageArra[] = { R.drawable.one, R.drawable.two, R.drawable.three, R.drawable.four, 
        			                    R.drawable.five,R.drawable.six, R.drawable.seven, R.drawable.eight,
        			                    R.drawable.nine,R.drawable.ten,R.drawable.eleven, R.drawable.twelve,
     
        			                    };
     
        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.myimage);        		
        		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.slide, 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.demo.SETTING");
                startActivity(p);
            break;
            }
     
        return false;
        }
           }

    Slide:

        public class Slide extends Activity {	
        	public int currentimageindex=0;
        	Timer timer;
        	TimerTask task;
        	ImageView slidingimage;
     
        	private int[] IMAGE_IDS = {
        			R.drawable.one, R.drawable.two, R.drawable.three,
        			R.drawable.four, R.drawable.five, R.drawable.six,
        			R.drawable.seven, R.drawable.eight, R.drawable.nine,
        			R.drawable.ten, R.drawable.eleven, R.drawable.twelve,
     
        			};
     
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                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_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_one == true) {	
           		slidingimage = (ImageView)findViewById(R.id.slide_image);
           		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.slide_image);
           		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.slide_image);
           		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.slide_image);
           		slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);  		
           		currentimageindex++;    	
            	Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.move);        
          	      slidingimage.startAnimation(rotateimage);   
     
            }else if (animation_five == true) {
            	slidingimage = (ImageView)findViewById(R.id.slide_image);
           		slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);  		
           		currentimageindex++;
          		Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.slide_down);       
            	  slidingimage.startAnimation(rotateimage);  
     
            }else if(animation_one == false && animation_two == false && animation_three == false 
            		&& animation_four == false && animation_five == false){
                Intent intent = new Intent(Slide.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.slide, 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.demo.SETTING");
           		    startActivity(p);
           		break;
           		}
     
           	return false; 
           	}
                     }

    Setting:

        public class Setting extends PreferenceActivity {
     
        	CheckBoxPreference animation_one;
        	CheckBoxPreference animation_two;
        	CheckBoxPreference animation_three;
        	CheckBoxPreference animation_four;
        	CheckBoxPreference animation_five;
     
        	@SuppressWarnings("deprecation")
        	protected void onCreate(Bundle savedInstanceState) {
        		// TODO Auto-generated method stub
        		super.onCreate(savedInstanceState);
        		addPreferencesFromResource(R.xml.setting);
     
        		animation_one = (CheckBoxPreference) findPreference("animation_one");
        		animation_two = (CheckBoxPreference) findPreference("animation_two");		
        		animation_three = (CheckBoxPreference) findPreference("animation_three");
        		animation_four = (CheckBoxPreference) findPreference("animation_four");
        		animation_five = (CheckBoxPreference) findPreference("animation_five");
     
     
        		animation_one.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
        		                @Override
        		                public boolean onPreferenceChange(Preference preference,
        		                        Object newValue) {
        		                    // TODO Auto-generated method stub
     
        				        	animation_one.setChecked(true);
        				        	animation_two.setChecked(false);
        				        	animation_three.setChecked(false);
        				        	animation_four.setChecked(false);
        				        	animation_five.setChecked(false);
     
        		                    return true;
        		                }
     
        		            });		    
        		animation_two.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
        		                @Override
        		                public boolean onPreferenceChange(Preference preference,
        		                        Object newValue) {
        		                    // TODO Auto-generated method stub
     
        				        	animation_one.setChecked(false);
        				        	animation_two.setChecked(true);
        				        	animation_three.setChecked(false);
        				        	animation_four.setChecked(false);
        				        	animation_five.setChecked(false);
     
        		                    return true;
     
        		                }
     
        		            });		    
        		                    /////
        		                    animation_three.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
        				                @Override
        				                public boolean onPreferenceChange(Preference preference,
        				                        Object newValue) {
        				                    // TODO Auto-generated method stub
     
        						        	animation_one.setChecked(false);
        						        	animation_two.setChecked(false);
        						        	animation_three.setChecked(true);
        						        	animation_four.setChecked(false);
        						        	animation_five.setChecked(false);
     
        				                    return true;
        				                }
     
        				            });		  
        		            animation_four.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
        				                @Override
        				                public boolean onPreferenceChange(Preference preference,
        				                        Object newValue) {
        				                    // TODO Auto-generated method stub
     
        						        	animation_one.setChecked(false);
        						        	animation_two.setChecked(false);
        						        	animation_three.setChecked(false);
        						        	animation_four.setChecked(true);
        						        	animation_five.setChecked(false);
     
        				                    return true;
        				                }
     
        				            });		  
     
        		                    animation_five.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
        				                @Override
        				                public boolean onPreferenceChange(Preference preference,
        				                        Object newValue) {
        				                    // TODO Auto-generated method stub
     
        						        	animation_one.setChecked(false);
        						        	animation_two.setChecked(false);
        						        	animation_three.setChecked(false);
        						        	animation_four.setChecked(false);
        						        	animation_five.setChecked(true);
     
        				                    return true;
        				                }
     
        				            });		  
     
        		   }
        		 }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I've created a slideshow using ViewPager, need to make a 3 small changes but stuck

    One of my first big java program years ago was a slideshow and image file manager. It used a java applet and back then it was possible to find a free web hosting site that allowed java applets. Now they are all gone. So I've posted my images on the Picasa Web Album site.
    Currently I working on learning Android and am looking for another project on Android. My first one was with Maps to show waypoints and routes. A slideshow might be something to help me learn some more. I don't know if I'd be able to help you much for a while but eventually I should get something useful.
    Could you put your project into a zip file, put it some site and PM me a link? I'd appreciate it.

    Norm
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Created my first Java program
    By ducksauce88 in forum The Cafe
    Replies: 3
    Last Post: September 22nd, 2013, 09:37 PM
  2. Stop Slideshow from starting automatically
    By kmk in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 6th, 2012, 07:21 AM
  3. Starting a Slideshow Program... Lost
    By DrumKitt87 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 1st, 2010, 07:31 AM
  4. quite small (make the changelog non-static, i.e. load its content from a file)
    By Abdallah in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: November 30th, 2009, 09:00 PM
  5. [SOLVED] Program to read from created file
    By aznprdgy in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 7th, 2009, 03:51 AM