hello friends
today we are going to learn about the slider in android. below code is used to create a slider in android. first, we need to add view pager in our android XML file and then we need to create one image adapter for view pager and then we can slide image easily in android. let's take a simple example.
activity_main.xml
<android.support.v4.view.ViewPager
android:id="@+id/slider"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="500dp" />
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
ViewPager slider = (ViewPager) findViewById(R.id.slider);
ImageAdapterForSlider imageadapter = new ImageAdapterForSlider(MainActivity.this);
slider.setAdapter(imageadapter);
}
public class ImageAdapterForSlider extends PagerAdapter {
Context Context;
ImageAdapterForSlider(Context context) {
this.Context = context;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
private int[] sliderImageId = new int[]{
R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4, R.drawable.image5,
};
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView img = new ImageView(Context);
img.setScaleType(ImageView.ScaleType.CENTER_CROP);
img.setImageResource(sliderImageId[position]);
((ViewPager) container).addView(img, 0);
return img;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
@Override
public int getCount() {
return sliderImageId.length;
}
}
}
today we are going to learn about the slider in android. below code is used to create a slider in android. first, we need to add view pager in our android XML file and then we need to create one image adapter for view pager and then we can slide image easily in android. let's take a simple example.
activity_main.xml
<android.support.v4.view.ViewPager
android:id="@+id/slider"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="500dp" />
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
ViewPager slider = (ViewPager) findViewById(R.id.slider);
ImageAdapterForSlider imageadapter = new ImageAdapterForSlider(MainActivity.this);
slider.setAdapter(imageadapter);
}
public class ImageAdapterForSlider extends PagerAdapter {
Context Context;
ImageAdapterForSlider(Context context) {
this.Context = context;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
private int[] sliderImageId = new int[]{
R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4, R.drawable.image5,
};
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView img = new ImageView(Context);
img.setScaleType(ImageView.ScaleType.CENTER_CROP);
img.setImageResource(sliderImageId[position]);
((ViewPager) container).addView(img, 0);
return img;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
@Override
public int getCount() {
return sliderImageId.length;
}
}
}
Comments
Post a Comment