Android Image Slider

Android image slider slides one entire screen to another screen. Image slider is created by ViewPager which is provided by support library. To implement image slider, you need to inherit ViewPager class which extends PagerAdapter.

Example of Image Slider

Let's see an example of android image slider.

activity_main.xml

In activity_main.xml file, we have wrapped ViewPager inside RelativeLayout.

File: activity_main.xml

Activity class

File: MainActivity.java

ImageAdapter class

Now create ImageAdapter class which extends PagerAdapter for android image slider.

Place some images in drawable folder which are to be slid.

File: ImageAdapter.java

We need to override following methods of PagerAdapter class.

  1. isViewFromObject(View, Object): This method checks the view whether it is associated with key and returned by instantiateItem().
  2. instantiateItem(ViewGroup, int): This method creates the page position passed as an argument.
  3. destroyItem(ViewGroup, int, Object): It removes the page from its current position from container. In this example we simply removed object using removeView().
  4. getCount(): It returns the number of available views in ViewPager.

Output

android Image Slider 1
android Image Slider 2




Latest Courses