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

24 47



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:

24 Comments
  1. KandiSAI Music says

    I have a set of 5 slideshow slides that are each different. I have an activity page with 5 buttons. I use a PagerAdapter to load one set of slides from one of the buttons. However, when I create a new PagerAdapter class for the second set of slides for the next button, It's loading the slides from the first button, even though I am loading the new slides from my drawable on the new PagerAdapter class.
    My question is, how can do my code where each button opens up a new set of slides using PagerAdapter? Because when I create a new PagerAdapter for the next button, the class is grayed out.

  2. aaron bolival says

    Sir my image from my database is 500px height and the width is 360 px. I want the image to fit in android how to code it?

  3. Hanaa Ibrahim says

    Thank you so so much, bro <3

  4. Thasreef Mogral says

    Thanks man you saved my day. I was working on image slider like playstore.this video helped me alot. Now its working

  5. FUGENO says

    Thanks for uplaoding

  6. Joseline Arellano says

    Why did you say anything ?
    You should teach us

  7. Raahim Hussain says

    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.

  8. Yara Bonilla says

    ¡Muchas gracias!, Thank u so much

  9. Mario Kramar says

    works fine, thanks for tutorial. how to make different sliders for each activity?

  10. Dellium says

    How can you add the dots that indicates which image you're on?

  11. Carlos Loyer Gaete says

    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.

  12. Thibaut Chieux says

    Nice tuto man, thx for the time 🙂

  13. Raj Rj says

    good job !!!

  14. Murtaza Amini says

    I get an out of memory error while sliding some images. How can I solve that?

  15. Ranjeet Bhoyar says

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

  16. Pencil Hands says

    Where did you get/put the 'slider1', 'slider2' and the 'slider3'???

  17. Dranzen Danganan says

    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?

  18. Zeeshan Mohiuddin says

    When run app will crash and show this error: FATAL EXCEPTION: main
    Process: com.example.zeeshan.protmotiftask, PID: 18184
    android.content.res.Resources$NotFoundException: Resource ID #0x7f060056

  19. Cik Yantok says

    hi i'm follow your instruction thats work but why my image position not same with your image slider ,

  20. I modified this on Array of Bitmap, write if u need it 🙂

  21. Gamer Mati Mati says

    How to set fit images

  22. aliffia permata says

    nice tutorial, thank you so much,

  23. Boss Gamer says

    cont have slide 2,3

  24. Yamilett Estrada-Reyes says

    I followed the code and didn't get any errors but my images won't show. Help please?

Leave A Reply

Your email address will not be published.