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 {



     @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



        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

    }


    @Override

    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;

    }


    @Override

    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"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity" >

<android.support.v4.view.ViewPager

android:id="@+id/view_pager"

android:layout_width="match_parent"

android:layout_height="match_parent" />

</RelativeLayout>

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

            R.drawable.second,

            R.drawable.third,

            R.drawable.fourth

    };

    ImageAdapter(Context context){

        this.context=context;

    }

    @Override

    public int getCount() {

        return GalImages.length;

    }  


    @Override

    public boolean isViewFromObject(View view, Object object) {

        return view == ((ImageView) object);

    }


    @Override

    public Object instantiateItem(ViewGroup container, int position) { 

        ImageView imageView = new ImageView(context);


        imageView.setImageResource(GalImages[position]);

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

        return imageView;

    }


    @Override

    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.