Creating a slideshow with ViewPager

This recipe will show you how to create a slideshow using the ViewPager class. Here is a screenshot showing a transition from one picture to another:

Creating a slideshow with ViewPager

Getting ready

Create a new project in Android Studio and call it: SlideShow. Use the default Phone & Tablet options and select Empty Activity when prompted for the Activity Type.

We need several images for the slideshow. For demonstration purposes, we downloaded four images from to include in the project source files, but you can use any images.

How to do it...

We'll create a Fragment to display each image for our slideshow, then set up the ViewPager

Get Android Application Development Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.