Friday, 25 September 2015

How to swipe images in android application

This is the tutorial regarding the swiping of multiple images in an android application.


1. Create a Java class 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. Create an xml file named as  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. Create another class called 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);
    }
}

Below is the video tutorial

You can download full source code for the above tutorial from the below link

Download Source Code


Learn and stay tuned and subscribe to my channel "Srce Cde." for more fun.

No comments:

Post a Comment