How to make ViewPager Example In Android

You can create swipe views in your application using the View Pager Fragment . In this tutorial included three example Cube Out Transformer, Dynamic Background, Image Gallery hope you are like this tutorial

1. MainActivity

import com.tutorialsee.cubeouttransformer.StaticBackgroundActivity;
import com.tutorialsee.dynamicbackground.DyanmicalBackground;
import com.tutorialsee.imagegallery.HomeActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class MainActivity extends Activity {
TextView background,viewpager,third;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        background = (TextView) findViewById(R.id.background);
        viewpager = (TextView) findViewById(R.id.viewpager);
        third = (TextView) findViewById(R.id.third);
       
        background.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent ii=new Intent(getBaseContext(),DyanmicalBackground.class);
ii.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(ii);

}
});
       
        viewpager.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent ii=new Intent(getBaseContext(),HomeActivity.class);
ii.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(ii);

}
});

        third.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent ii=new Intent(getBaseContext(),StaticBackgroundActivity.class);
ii.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(ii);

}
}); 
    }
}

2. Home Activity

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;

import com.tutorialsee.viewpagerandroid.R;

public class HomeActivity extends AppCompatActivity {

    private ArrayList<Integer> images;
    private BitmapFactory.Options options;
    private ViewPager viewPager;
    private View btnNext, btnPrev;
    private FragmentStatePagerAdapter adapter;
    private LinearLayout thumbnailsContainer;
    private final static int[] resourceIDs = new int[]{R.drawable.a, R.drawable.b,
            R.drawable.c, R.drawable.d, R.drawable.e, R.drawable.f, R.drawable.g};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        images = new ArrayList<>();

        //find view by id
        viewPager = (ViewPager) findViewById(R.id.view_pager);
        thumbnailsContainer = (LinearLayout) findViewById(R.id.container);
        btnNext = findViewById(R.id.next);
        btnPrev = findViewById(R.id.prev);

        btnPrev.setOnClickListener(onClickListener(0));
        btnNext.setOnClickListener(onClickListener(1));

        setImagesData();

        // init viewpager adapter and attach
        adapter = new ViewPagerAdapter(getSupportFragmentManager(), images);
        viewPager.setAdapter(adapter);

        inflateThumbnails();
    }

    private View.OnClickListener onClickListener(final int i) {
        return new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (i > 0) {
                    //next page
                    if (viewPager.getCurrentItem() < viewPager.getAdapter().getCount() - 1) {
                        viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
                    }
                } else {
                    //previous page
                    if (viewPager.getCurrentItem() > 0) {
                        viewPager.setCurrentItem(viewPager.getCurrentItem() - 1);
                    }
                }
            }
        };
    }

    private void setImagesData() {
        for (int i = 0; i < resourceIDs.length; i++) {
            images.add(resourceIDs[i]);
        }
    }

    private void inflateThumbnails() {
        for (int i = 0; i < images.size(); i++) {
            View imageLayout = getLayoutInflater().inflate(R.layout.item_image, null);
            ImageView imageView = (ImageView) imageLayout.findViewById(R.id.img_thumb);
            imageView.setOnClickListener(onChagePageClickListener(i));
            options = new BitmapFactory.Options();
            options.inSampleSize = 3;
            options.inDither = false;
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), images.get(i), options );
            imageView.setImageBitmap(bitmap);
            //set to image view
            imageView.setImageBitmap(bitmap);
            //add imageview
            thumbnailsContainer.addView(imageLayout);
        }
    }

    private View.OnClickListener onChagePageClickListener(final int i) {
        return new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(i);
            }
        };
    }
}

3. DyanmicalBackground

import com.tutorialsee.viewpagerandroid.R;
import android.support.v4.view.PagerAdapter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class DyanmicalBackground extends AppCompatActivity {

private static final int MAX_PAGES = 10;
private DynamicViewPager viewPager;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dyanmical_background);

viewPager = (DynamicViewPager) findViewById(R.id.pager);
viewPager.setMaxPages(MAX_PAGES);
viewPager.setBackgroundAsset(R.drawable.background);
viewPager.setAdapter(new MyPagerAdapter());

}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);

}

private class MyPagerAdapter extends PagerAdapter {
@Override
public int getCount() {
return MAX_PAGES;
}

@Override
public boolean isViewFromObject(View view, Object o) {
return view == o;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.layout_page, null);
TextView num = (TextView) view.findViewById(R.id.page_number);
String pos = "This is tutorialsee " + (position + 1);
num.setText(pos);

container.addView(view);

return view;
}
}
}

4. Cube Out Transformer

import android.annotation.TargetApi;
import android.os.Build;
import android.support.v4.view.ViewPager;
import android.view.View;

public class CubeOutTransformer implements ViewPager.PageTransformer
{
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public void transformPage(View view, float position) {
        final float rotation = (position < 0 ? 90f : -90f) * Math.abs(position);
        view.setAlpha(rotation > 90f || rotation < -90f ? 0f : 1f);
        view.setPivotX(position < 0f ? view.getWidth() : 0f);
        view.setPivotY(view.getHeight() * 0.5f);
        view.setRotationY(90f * position);
    }
}