How to make Image Slider with ViewPager in Android Studio | Sanktips
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:
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.
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?
Thank you so so much, bro <3
Thanks man you saved my day. I was working on image slider like playstore.this video helped me alot. Now its working
Thanks for uplaoding
Why did you say anything ?
You should teach us
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.
¡Muchas gracias!, Thank u so much
works fine, thanks for tutorial. how to make different sliders for each activity?
How can you add the dots that indicates which image you're on?
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.
Nice tuto man, thx for the time 🙂
good job !!!
I get an out of memory error while sliding some images. How can I solve that?
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference
this problem solve
Where did you get/put the 'slider1', 'slider2' and the 'slider3'???
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?
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
hi i'm follow your instruction thats work but why my image position not same with your image slider ,
I modified this on Array of Bitmap, write if u need it 🙂
How to set fit images
nice tutorial, thank you so much,
cont have slide 2,3
I followed the code and didn't get any errors but my images won't show. Help please?