Looking for swiping an images in your Android Application? Yes; you are at right place. Try displaying images in grid view format too. Here, in this tutorial you would learn that how swiping of images takes place behind the scene. Well, here i have used Android Studio for the example.. By going through this you will successfully able to swipe images one after another in your Android application. Without diving in depth theoretical part, i would directly take you to the source code. Below is the source code for the tutorial.

1) MainActivity.java

package com.spinner.swipe.swipe;

import android.support.v4.view.ViewPager;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.util.Log;

import android.view.Menu;

import android.view.MenuItem;

import android.widget.Toast;

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.URL;

public class MainActivity extends AppCompatActivity {


    protected void onCreate(Bundle savedInstanceState) {



        ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);   

        ImageAdapter adapter = new ImageAdapter(this); //Here we are defining the Imageadapter object

        viewPager.setAdapter(adapter); // Here we are passing and setting the adapter for the images



    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.

        getMenuInflater().inflate(R.menu.menu_main, menu);

        return true;



    public boolean onOptionsItemSelected(MenuItem item) {

        // Handle action bar item clicks here. The action bar will

        // automatically handle clicks on the Home/Up button, so long

        // as you specify a parent activity in AndroidManifest.xml.

        int id = item.getItemId();

        //noinspection SimplifiableIfStatement

        if (id == R.id.action_settings) {

            return true;


        return super.onOptionsItemSelected(item);



2) activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"




tools:context=".MainActivity" >




android:layout_height="match_parent" />


3) ImageAdapter.java

package com.spinner.swipe.swipe;

import android.content.Context;

import android.support.v4.view.PagerAdapter;

import android.support.v4.view.ViewPager;

import android.view.View;

import android.view.ViewGroup;

import android.widget.ImageView;


 * Created by SrceCde on 18-09-2015.


public class ImageAdapter extends PagerAdapter {

    Context context;

    private int[] GalImages = new int[] {

            R.drawable.first,    //Here first,second,third... are the name of the jpeg files placed in drawable folder





    ImageAdapter(Context context){




    public int getCount() {

        return GalImages.length;



    public boolean isViewFromObject(View view, Object object) {

        return view == ((ImageView) object);



    public Object instantiateItem(ViewGroup container, int position) { 

        ImageView imageView = new ImageView(context);


        ((ViewPager) container).addView(imageView, 0);

        return imageView;



    public void destroyItem(ViewGroup container, int position, Object object) { 

        ((ViewPager) container).removeView((ImageView) object);



Also you can refer the video tutorial for the same.

Download Source code

Hope this article helped in swiping of images in the Android Application. Follow me on Twitter

Keep learning. Stay tuned to Tech Tunes.