Image Slider in Android using ViewPager Last Updated : 06 Aug, 2025 Comments Improve Suggest changes 8 Likes Like Report 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 AndroidStep 1: Create a New ProjectTo 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 UIBelow 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 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <!-- viewpager to show images --> <androidx.viewpager.widget.ViewPager android:id="@+id/viewPagerMain" android:layout_width="match_parent" android:layout_height="match_parent"/> </RelativeLayout> 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 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- image viwer to view the images --> <ImageView android:id="@+id/imageViewMain" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout> Step 3: Coding PartFirst, 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. Java 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); } @Override public int getCount() { // return the number of images return images.length; } @Override public boolean isViewFromObject(@NonNull View view, @NonNull Object object) { return view == ((LinearLayout) object); } @NonNull @Override 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(R.id.imageViewMain); // setting the image in the imageView imageView.setImageResource(images[position]); // Adding the View Objects.requireNonNull(container).addView(itemView); return itemView; } @Override 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 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. Java import androidx.appcompat.app.AppCompatActivity; 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; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Initializing the ViewPager Object mViewPager = (ViewPager)findViewById(R.id.viewPagerMain); // Initializing the ViewPagerAdapter mViewPagerAdapter = new ViewPagerAdapter(MainActivity.this, images); // Adding the Adapter to the ViewPager mViewPager.setAdapter(mViewPagerAdapter); } } Output: Run on EmulatorAdditional Links:Download the Full Project From the LinkDownload Images Using in This Project Download The App Create Quiz Comment O onlyklohan Follow 8 Improve O onlyklohan Follow 8 Improve Article Tags : Android Android Projects Explore BasicsIntroduction to Android Development 5 min read History of Android 15+ min read Best Way to Become Android Developer â A Complete Roadmap 7 min read Android Development Prerequisites [2025] - Things to Learn Before Android Development 8 min read Android App Development Fundamentals for Beginners 6 min read Android Architecture 5 min read Android System Architecture 3 min read Android Boot Process 4 min read Difference between Java and Kotlin in Android with Examples 3 min read Interesting Facts About Android 3 min read Software Setup and ConfigurationDownload and Install JDK on Windows, Mac and Linux 6 min read Guide to Install and Setup IntelliJ IDEA for Android App Development 5 min read Guide to Install and Setup Visual Studio for Android App Development 4 min read How to Run the Android App on a Real Device? 2 min read Resolving frequently occurring errors in Android Development 3 min read Android Studio Tutorial 9 min read File Structure & ComponentsComponents of an Android Application 3 min read Introduction to Activities in Android 6 min read Services in Android with Example 10 min read Core TopicsHow Does Android App Work? 7 min read Activity Lifecycle in Android with Demo App 9 min read Introduction to Gradle 4 min read What is Context in Android? 9 min read Bundle in Android with Example 6 min read Activity State Changes In Android with Example 6 min read Processes and Application Lifecycle in Android 7 min read Desugaring in Android 4 min read Difference Between AndroidX and Android Support Libraries 3 min read Memory Leaks in Android 7 min read Layout & ViewLayouts in Android UI Design 3 min read Android UI Layouts 5 min read LinearLayout and its Important Attributes with Examples in Android 3 min read Android LinearLayout in Kotlin 2 min read Android RelativeLayout in Kotlin 4 min read ConstraintLayout in Android 6 min read TextView widget in Android with Examples 5 min read TextView in Kotlin 3 min read Working With the TextView in Android 7 min read Autosizing TextView in Android 6 min read ButtonButton in Android 3 min read How to Add Radio Buttons in an Android Application? 5 min read RadioButton in Kotlin 4 min read How to add Toggle Button in an Android Application 3 min read ToggleButton in Kotlin 2 min read RadioGroup in Kotlin 3 min read Intent and Intent FiltersWhat is Intent in Android? 4 min read Implicit and Explicit Intents in Android with Examples 6 min read How to Send Data From One Activity to Second Activity in Android? 7 min read How to open dialer in Android through Intent? 3 min read Creating Multiple Screen Applications in Android 6 min read How to Open Camera Through Intent and Display Captured Image in Android? 6 min read Toast & RecyclerViewToasts for Android Studio 2 min read What is Toast and How to Use it in Android with Examples? 6 min read Android Toast in Kotlin 3 min read How to Change Toast font in Android? 3 min read How to add a custom styled Toast in Android 4 min read RecyclerView in Android with Example 7 min read Android | Horizontal RecyclerView with Examples 4 min read How to create a nested RecyclerView in Android 5 min read How to Create RecyclerView with Multiple ViewType in Android? 6 min read RecyclerView using ListView in Android With Example 5 min read Fragments & AdaptersIntroduction to Fragments | Android 5 min read Fragment Lifecycle in Android 8 min read How to Create a New Fragment in Android Studio? 2 min read How to Create Swipe Navigation in Android? 6 min read ViewPager Using Fragments in Android with Example 6 min read ArrayAdapter in Android with Example 3 min read SimpleAdapter in Android with Example 7 min read SimpleExpandableListAdapter in Android with Example 10 min read AdapterViewFlipper in Android with Example 5 min read BaseExpandableListAdapter in Android with Example 10 min read Like