Image Slider android

To slide one entire screen to another screen, the image slider is used in Android which is created by the ViewPager. The support library provides the ViewPager. The ViewPager class must be inherited to implement an image slider. It extends the PagerAdapter.

Example of Image Slider:

activity_main.xml:

The ViewPager is wrapped inside the RelativeLayout, in the activity_main.xml file.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
 
   tools:context="MainActivity">
 
   <android.support.v4.view.ViewPager
       android:id="@+id/viewPage"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent" />
 
</RelativeLayout>

Activity class:(File: MainActivity.java)

package com.example.radioapp;
 
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
 
public class MainActivity extends AppCompatActivity {
 

ImageAdapter class:(File: ImageAdapter.java:)

To extend the PagerAdapter for an android image slider, we will create the ImageAdapter class. While the images to be slid are placed in the drawable folder.

package com.example.radioapp;
 
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;
 
public class ImageAdapter extends PagerAdapter{
   Context mContext;
 
   ImageAdapter(Context context) {
       this.mContext = context;
   }
 
   @Override
   public boolean isViewFromObject(View view, Object object) {
       return view == ((ImageView) object);
   }
 
   private int[] sliderImageId = new int[]{
           R.drawable.java, R.drawable.php
   };