Open In App
Related Articles

Image Slider in Android using ViewPager

Improve Article
Save Article
Like Article

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.

Image slider

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.


<?xml version="1.0" encoding="utf-8"?>
  <!-- viewpager to show images -->

  • 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.


<?xml version="1.0" encoding="utf-8"?>
    <!-- image viwer to view the images -->

Step 3: Coding Part

  • First, create an Adapter for the ViewPager and named it as ViewPagerAdapter class below is the complete code of class. Comments are added inside the code to understand each line of the code.


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import java.util.Objects;
class ViewPagerAdapter extends PagerAdapter {
    // Context object 
    Context context;
    // Array of images
    int[] images;
    // Layout Inflater
    LayoutInflater mLayoutInflater;
    // Viewpager Constructor 
    public ViewPagerAdapter(Context context, int[] images) {
        this.context = context;
        this.images = images;
        mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    public int getCount() {
        // return the number of images
        return images.length;
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == ((LinearLayout) object);
    public Object instantiateItem(@NonNull ViewGroup container, final int position) {
        // inflating the item.xml 
        View itemView = mLayoutInflater.inflate(R.layout.item, container, false);
        // referencing the image view from the item.xml file 
        ImageView imageView = (ImageView) itemView.findViewById(;
        // setting the image in the imageView
        // Adding the View
        return itemView;
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((LinearLayout) object);

  • After creating the Adapter for the ViewPager, reference the ViewPager from the XML and set the adapter to it in the file. Create an array of integer which contains the images which we will show in the ViewPager. Below is the complete code for the file. Comments are added inside the code to understand each line of the code.


import androidx.viewpager.widget.ViewPager;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
    // creating object of ViewPager
    ViewPager mViewPager;
    // images array
    int[] images = {R.drawable.a1, R.drawable.a2, R.drawable.a3, R.drawable.a4,
                    R.drawable.a5, R.drawable.a6, R.drawable.a7, R.drawable.a8};
    // Creating Object of ViewPagerAdapter
    ViewPagerAdapter mViewPagerAdapter;
    protected void onCreate(Bundle savedInstanceState) {
        // Initializing the ViewPager Object
        mViewPager = (ViewPager)findViewById(;
        // Initializing the ViewPagerAdapter
        mViewPagerAdapter = new ViewPagerAdapter(MainActivity.this, images);
        // Adding the Adapter to the ViewPager

Output: Run on Emulator

Additional Links:

  • Download the Full Project From the Link
  • Download Images Using in This Project 
  • Download The App

Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
  • Comprehensive Course
  • Expert Guidance for Efficient Learning
  • Hands-on Experience with Real-world Projects
  • Proven Track Record with 100,000+ Successful Geeks

Last Updated : 15 Sep, 2020
Like Article
Save Article
Similar Reads
Complete Tutorials