How to make Image Slider with ViewPager in Android Studio | Sanktips

Google+ Pinterest LinkedIn Tumblr +

Video is ready, Click Here to View ×


In this video, we will see how to make an Image Slider with ViewPager in Android Studio. As we know Image Slider is the most popular thing, Even Google PlayStore uses image slider on its home page. As an Android Developer you should know how to implement this feature in your app.

An issues while making image slider comment down below…

Like ✔ Comment ✔ Share ✔

Follow Us On:

Share.

About Author

34 Comments

  1. sir how could we use the images to open a new activity?,i have seen your video in which you had shown to make the images clickable but i could not use startactivity() in the "if" condition.

  2. Thanks a lot buddy, you saved me a lot.
    I would like to leave my contribution: If you want to use the PagerAdapter with an array of images brought from a query and decoded with base64 (if you aren't able to get the images from drawable folder), you has to do the following:

    – First, decode base64 image as byte:
    byte[] imageAsBytes1 = Base64.decode(json.getString("bigblob").getBytes("UTF-8"), 0);
    byte[] imageAsBytes2 = Base64.decode(json.getString("bigblob2").getBytes("UTF-8"), 0);

    – Then, decode the byte array as bitmap:
    Bitmap bm1 = BitmapFactory.decodeByteArray(imageAsBytes1, 0, imageAsBytes1.length);

    Bitmap bm2 = BitmapFactory.decodeByteArray(imageAsBytes2, 0, imageAsBytes2.length);

    – Create the bitmap array:
    Bitmap[] imgs = {bm1, bm2};

    – Finally, create the instance of the pageradapter with the context as the video said, and then, load the array through a "setter" created on the ViewPagerAdapter.java class:

    ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(InfoActivity.this);

    viewPagerAdapter.setImages(imgs);

    * Don't forget to set the adapter:
    viewPager.setAdapter(viewPagerAdapter);

    ———————————————————————————————————————————
    Another thing, you has to change some stuff on the ViewPagerAdapter.java class:

    1.- The array datatype: private Bitmap[] images = {};
    2.- On the @Override instantiateItem, change the "imageView.setImageResource(images[position]);" to -> "imageView.setImageBitmap(images[position]);"
    3.- Create the setter and getters, so you will be able to send the bitmap array to the ViewPagerAdapter as I said on the "1.-".

    One more time, thanks a lot buddy, it's nice to learn from people like you.

  3. java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference
    this problem solve

  4. java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference can you help me with this error?

  5. hii great video, I need a little help on image slider I have to make a project by tommorow , i want to make a quizz app and i want to impliment slider for asking questions , but when image is slided i need to tick correct ot wrong how to do it , is there any way to contact you

  6. sir i have some error everytime i create a java new class and i type a ViewPagerAdapter it is not working how i fix this sir plss help me i follow your tutorial but when i create a java class after Typing the ViewPagerAdapter it is not work the message is error templates what should ido sir ??

  7. sir,
    i am getting error in this MainActivity when i write ,
    ViewPagerAdapter viewpageradapter = new ViewPagerAdapter(this);

    here there's an error on"this"

    kindly help

  8. Thank you, although you don't say any word, it is the best tutorial I could find in YouTube. I hope you keep making more Tutorials