When talking about Android Apps, the first thing that comes to mind is variety. There are so many varieties of Android apps providing the user with beautiful dynamic UI. One such feature is to navigate in the Android Apps using the left and right swipes as opposed to clicking on Buttons. Not only does it look more simple and elegant but also provides ease of access to the user. There are many apps that use this swipe feature to swipe through different activities in the app. For example, the popular chatting app, Snapchat, uses it to swipe through lenses, chats, and stories. Here let’s discuss how to create an Image Slider using ViewPager. ViewPager is a class in Java that is used in conjunction with Fragments. It is mostly used for designing the UI of the app. A sample GIF is given below to get an idea about what we are going to do in this article.
Steps for Creating Image Slider in Android
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Designing the UI
- Below is the code for the activity_main.xml file. We have added only a ViewPager to show the images. Below is the complete code for the activity_main.xml file.
- Create a new Layout Resource File item.xml inside the app -> res -> layout folder. Add only an ImageView. Below is the code of the item.xml file.
Step 3: Coding Part
- First, create an Adapter for the ViewPager and named it as ViewPagerAdapter class below is the complete code of ViewPagerAdapter.java class. Comments are added inside the code to understand each line of the code.
- After creating the Adapter for the ViewPager, reference the ViewPager from the XML and set the adapter to it in the MainActivity.java file. Create an array of integer which contains the images which we will show in the ViewPager. Below is the complete code for the MainActivity.java file. Comments are added inside the code to understand each line of the code.
Output: Run on Emulator
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- How to implement a TabLayout in Android using ViewPager and Fragments
- How to Implement Tabs, ViewPager and Fragment in Android using Kotlin?
- ViewPager Using Fragments in Android with Example
- How to Create a Color Picker Tool in Android using Color Wheel and Slider?
- JavaFX | Slider Class
- Image Processing in Java | Set 3 (Colored image to greyscale image conversion)
- Image Processing in Java | Set 4 (Colored image to Negative image conversion)
- Image Processing in Java | Set 6 (Colored image to Sepia image conversion)
- How to install Android Applications in Mobile Phone without USB Cables using Android Studio
- How to Add OpenCV library into Android Application using Android Studio?
- How to create a Circular image view in Android without using any library?
- Android | Android Application File Structure
- Android | AdMob Interstitial Ads for Android Studio
- Android | How to Create/Start a New Project in Android Studio?
- Android | Running your first Android app
- Android | How to add Radio Buttons in an Android Application?
- Android | How to Request permissions in Android Application?
- Difference between Android 1.1 and Android 4.0.1
- Difference between Android 1.1 and Android 2.3.4
- Difference between Android 1.0 and Android 1.1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.