Sunday, February 4, 2018

Android Image Slider Example

In this tutorial, we will create an android image slider using android view pager and CircleIndicator library.

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




main.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:paddingBottom="@dimen/activity_vertical_margin"  
  7.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  8.     android:paddingRight="@dimen/activity_horizontal_margin"  
  9.     android:paddingTop="@dimen/activity_vertical_margin"  
  10.     tools:context="com.example.test.imageslider.MainActivity">  
  11.   
  12.   
  13.     <android.support.v4.view.ViewPager  
  14.         android:id="@+id/viewPage"  
  15.         android:layout_width="fill_parent"  
  16.         android:layout_height="fill_parent" />  
  17.   
  18. </RelativeLayout> 



MainActivity


  1. public class MainActivity extends AppCompatActivity {  
  2.   
  3.     @Override  
  4.     protected void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.main);  
  7.   
  8.         ViewPager mViewPager = (ViewPager) findViewById(R.id.viewPage);  
  9.         ImageAdapter adapterView = new ImageAdapter(this);  
  10.         mViewPager.setAdapter(adapterView);  
  11.     }  
  12. }  


ImageAdapter.java


  1. public class ImageAdapter extends PagerAdapter{  
  2.     Context mContext;  
  3.   
  4.     ImageAdapter(Context context) {  
  5.         this.mContext = context;  
  6.     }  
  7.   
  8.     @Override  
  9.     public boolean isViewFromObject(View view, Object object) {  
  10.         return view == ((ImageView) object);  
  11.     }  
  12.   
  13.     private int[] sliderImageId = new int[]{  
  14.             R.drawable.image1, R.drawable.image2, R.drawable.image3,R.drawable.image4, R.drawable.image5,  
  15.     };  
  16.   
  17.     @Override  
  18.     public Object instantiateItem(ViewGroup container, int position) {  
  19.         ImageView imageView = new ImageView(mContext);  
  20.         imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);  
  21.         imageView.setImageResource(sliderImageId[position]);  
  22.         ((ViewPager) container).addView(imageView, 0);  
  23.         return imageView;  
  24.     }  
  25.   
  26.     @Override  
  27.     public void destroyItem(ViewGroup container, int position, Object object) {  
  28.         ((ViewPager) container).removeView((ImageView) object);  
  29.     }  
  30.   
  31.     @Override  
  32.     public int getCount() {  
  33.         return sliderImageId.length;  
  34.     }  
  35. }  

No comments:

Post a Comment