Would you like to create an Android Image Slider Control for your App?
At Allcode, we needed to build an Image Slider Control into one of our client’s Android apps. Initially, we thought it would be as simple as implementing the standard Android Gallery Widget,
http://developer.android.com/reference/android/widget/Gallery.html
Unfortunately, the Gallery Control has been deprecated, which means that it won’t work on new devices.
After digging around a little bit more, we found that most of the implementations were based upon the the ViewPager,
http://developer.android.com/reference/android/support/v4/view/ViewPager.html
The ViewPager has problems as well. First, it’s not supported on old devices. Second, since the the ViewPager is new, it stated that “”Note this class is currently under early design and development. The API will likely change in later updates of the compatibility library, requiring changes to the source code of apps when they are compiled against the newer version.” Yuck.
So at Allcode, we built our own Image Slider based upon the HorizontalScrollView. The first part was easy. We just created a HorizontalScrollView to hold a Horizontal Linear Layout with the slider items. The Option class is just a bean that has a name and a URL. The DownloadImageTask is an asynchronous task that will handle loading and caching the images on an ImageView.
public class ImageSlider extends HorizontalScrollView { private List images = new LinkedList(); private LinearLayout layout; private int currentSelectedIndex = 0; public ImageSlider(Context context) { super(context); init(context); } public ImageSlider(Context context, List images) { super(context); this.images = images; init(context); } void init(Context context) { setHorizontalFadingEdgeEnabled(false); setVerticalFadingEdgeEnabled(false); layout = new LinearLayout(context); LinearLayout.LayoutParams lParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); layout.setLayoutParams(lParams); this.addView(layout); for(Option image: images){ View sliderItem = ((Activity)context).getLayoutInflater().inflate(R.layout.slider_item, layout, false); ImageView tmpImage = (ImageView)sliderItem.findViewById(R.id.sliderImage); tmpImage.setContentDescription(image.getName()); TextView tmpText = (TextView)sliderItem.findViewById(R.id.sliderText); tmpText.setText(image.getName()); DownloadImageTask imageTask = new DownloadImageTask(tmpImage); imageTask.execute(image.getValue()); layout.addView(sliderItem); } } }
We also created the layout for each of the slider items as follows:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <ImageView android:id="@+id/sliderImage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:adjustViewBounds="true" android:scaleType="centerInside" android:padding="5dp" /> <TextView android:id="@+id/sliderText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" /> </LinearLayout>
Our next post will detail some of the issues that we had in adding this ImageSlider to a view.