0

I'm creating an app on android for viewing the image when I select the next button to show the new image and the back button to show previous image but when the press the next button to the maximum index , i want it to show the start from first image index and when the press the back button to the minimum index, i want it to show the start from maxminum image index. I have problem when i click the back button go to 0 index i want it show the image from the final index and also when i click the next button go to final index i want it show the from the 0 index. *

    if (v == btn_back) {
        if((currentimageindex)!=0){
            imageview.setImageResource(IMAGE_IDS[currentimageindex]);
               mp = MediaPlayer.create(this, myMusic[currentsoundindex]);
               mp.start();
              --currentimageindex;
              --currentsoundindex;
           }
          if((currentimageindex)==0){
                    imageview.setImageResource(IMAGE_IDS[currentimageindex]);
                       mp = MediaPlayer.create(this, myMusic[currentsoundindex]);
                       mp.start();
              --currentimageindex;
              --currentsoundindex;}
    if(v== btn next){*******}

1 Answer 1

2

Replace --currentimageindex with:

currentimageindex = (currentimageindex + IMAGE_IDS.length -1) % IMAGE_IDS.length;

When the index is 0 this will make it will wrap around to IMAGE_IDS.length-1.

Note, for going forward you only need do this:

currentimageindex = (currentimageindex + 1) % IMAGE_IDS.length;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.