0

So, I want to create an Image Switcher which contains two button 'next' and 'previous'. Whenever I click on the 'next' button it will switch to the next image and 'previous' button will switch to the previous image. I am using an array to store all the images url, let's say I have image1 and when I click on the 'next' button it will switch to image2 and so on. For now I can only switch between two images, here's my code.

Update: I had updated my code, and this time I can switch between images but the app crashes at some time. Any ideas?

XML

<ImageSwitcher
    android:id="@+id/images"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"/>


<ImageButton
    android:id="@+id/btnleft"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="23dp"
    android:layout_marginStart="90dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true"
    android:src="@android:drawable/ic_media_rew"
    android:background="@android:color/holo_green_light"/>

<ImageButton
    android:id="@+id/btnRight"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="91dp"
    android:layout_alignTop="@+id/btnleft"
    android:layout_alignParentEnd="true"
    android:src="@android:drawable/ic_media_ff"
    android:background="@android:color/holo_green_light"/>

Main.java

public class MainActivity extends AppCompatActivity {

private ImageSwitcher imageSwitcher;
private ImageButton buttonLeft, buttonRight;
public static int currentIndex=0;
private static String url[] = new String[]{
        "https://pbs.twimg.com/profile_images/875443327835025408/ZvmtaSXW_400x400.jpg",
        "http://www.ipbrief.net/wp-content/uploads/2012/05/262px-Android_dance.svg_.png",
        "http://quizzzz.net/en/logo/images/1342886140.jpg",
        "https://androiddevsimplified.files.wordpress.com/2016/01/android-logo-png-05073.png"
};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    imageSwitcher = (ImageSwitcher)findViewById(R.id.images);

    buttonLeft = (ImageButton)findViewById(R.id.btnleft);

    buttonRight = (ImageButton)findViewById(R.id.btnRight);

    imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
        @Override
        public View makeView() {
            // Returns the context for the entire application (the process all the Activities are running inside of)
            ImageView imageView = new ImageView(getApplicationContext());

            imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);

            imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
            return imageView;
        }
    });

    imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left));
    imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right));


    buttonRight.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(currentIndex < url.length)
                LoadingImage(url[currentIndex++]);
        }
    });

    buttonLeft.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           if(currentIndex > 0)
               LoadingImage(url[currentIndex--]);
        }
    });
}

private void LoadingImage(final String url){
    ImageLoader imageLoader = Singleton.getInstance(getApplicationContext()).getImageLoader();
    imageLoader.get(url, new ImageLoader.ImageListener(){

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("Error", error.getMessage());

        }

        @Override
        public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
            Bitmap image = response.getBitmap();
            Drawable drawable = new BitmapDrawable(getResources(),image);
            imageSwitcher.setImageDrawable(drawable);
        }
    });
}

}

CodeX
  • 35
  • 8

2 Answers2

0

I didn't really know which problem you are facing. You want to change image from the list, but you only loadImage with index 0 and 1 in your array.

Use a global variable to store current image index.

currIndex = 0;

btnNext.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if(currIndex < url.length() - 1) {
            loadingImage(url[currIndex],0);
            imgView[1].setImageResource(0);
        }
    }
});

btnPrevious.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if(currIndex > 0) {
            loadingImage(url[currIndex],1);
            imgView[0].setImageResource(0);
        }
    }
});
nhoxbypass
  • 9,695
  • 11
  • 48
  • 71
-2

Change from imageLoader.get(url) to loadurl(url) as per the new API.

Change

imageLoader.get(url, new ImageLoader.ImageListener() {
            @Override
            public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
                Bitmap img = response.getBitmap();
                imgView[i].setImageBitmap(img);
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("Error",error.getMessage());

            }
        });

To

// Load image, decode it to Bitmap and return Bitmap to callback
        imageLoader.loadImage(url, new SimpleImageLoadingListener() {
            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                // Do whatever you want with Bitmap
                imgView[i].setImageBitmap(img);
            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {

                Log.e("Error", failReason.getCause().getMessage());
            }
        });
Ronny K
  • 3,641
  • 4
  • 33
  • 43
  • Answers consisting of nothing but *Try this* and a code dump are not useful here, because they impart no knowledge. Why should the poster *try this*? How specifically does it address the problem, and why does it fix it? This site is for sharing knowledge and instructing people, not providing copy/paste answers with no other information. See [this Meta StackExchange post](https://meta.stackexchange.com/q/196187/172661) – Ken White Nov 12 '17 at 03:51
  • Ok i get it. I just created an app using the code provided by the op. I've edited the answer to explain where the issue is – Ronny K Nov 12 '17 at 03:55
  • I don't see anything you've added that *explains* anything. Why is that change relevant? How specifically does it fix the problem? If you're not willing to explain how your answer is a solution, it's not an answer. Please read the link I provided. Also see specifically [this answer to that Meta SE question](https://meta.stackexchange.com/a/196191/172661) – Ken White Nov 12 '17 at 03:56
  • You've added no further explanation. You're not even reading what I've written. You're making no attempt to provide an explanation; you're just providing copy/paste code. This site is for **sharing knowledge and helping educate others**. Copy/paste *use this* doesn't provide any knowledge or provide any education to anyone; it simply provides code. – Ken White Nov 12 '17 at 04:04