Android ViewPager as Image Slider Example/Tutorial

Working with ViewPager to show image Slider. Following are required:-

  1. 1  Activity class containing viewpager.
    2  Adapter class to be set to viewpager.
    3  Fragment class whose view will be displayed in viewpager

//MainActivity containing viewpager

MainActivity.java

package com.androidruler.imageslider;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    ViewPager pager;
    List<Fragment> fragments;
    // list containing id's of images we want to show in viewpager 
    // images are in drawable folder
    ArrayList<Integer> images=new ArrayList<Integer>(){{
        add(R.drawable.wonder1);
        add(R.drawable.wonder2);
        add(R.drawable.wonder3);
        add(R.drawable.wonder4);
        add(R.drawable.wonder5);
        add(R.drawable.wonder6);
        add(R.drawable.wonder7);
    }};
    PagerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         pager = (ViewPager) findViewById(R.id.viewPager);
        
        //method to create the required fragments to show in viewpager and return as a fragment list
        fragments=getFragments();
        adapter=new PagerAdapter(getSupportFragmentManager(),fragments);
        pager.setAdapter(adapter);
    }

    @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);
    }

// Adapter class which is set to viewpager

PagerAdapter.java

   class PagerAdapter extends FragmentPagerAdapter {

        private List<Fragment> fragments;
        public PagerAdapter(FragmentManager fm, List<Fragment> fragments) {
            super(fm);
            this.fragments = fragments;
        }

        @Override
       public Fragment getItem(int position) {
            return this.fragments.get(position);
        }

        @Override
        public int getCount() {
            return this.fragments.size();
        }
    }

    
    //method to get list of fragments to be displayed in viewpager 
    private List<Fragment> getFragments() {
        List<Fragment> fList = new ArrayList<Fragment>();
        for (int i = 0; i < images.size(); i++) {
            fList.add(MyImageSlider.newInstance(images.get(i)));
        }
        return fList;
    }
}

//Fragment class

MyImageSlider.java

package com.androidruler.imageslider;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

// its the fragment from where we create fragments
public class MyImageSlider extends Fragment {

    int imageid;

// static method to create the MyImageSlider Fragment containing image
    public  static MyImageSlider newInstance(int id)
    {
        MyImageSlider slider=new MyImageSlider();
        Bundle b=new Bundle();
        b.putInt("imageid", id);
        slider.setArguments(b);
        return slider;
    }


// get the image id from fragment in this method although we can also get in onCreateView.
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        imageid=getArguments().getInt("imageid");
    }


// this method returns the view containing the required which is set while creating instance of fragment
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.adapterview, container, false);
        ImageView iv=(ImageView)view.findViewById(R.id.myimage);

        iv.setImageResource(imageid);

        return view;
    }
}

//layout for MainActivity.java

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/viewPager"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</RelativeLayout>

//layout for MyImageSlider

adapterview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/myimage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

Output

Screenshot_2016-02-27-18-14-28