a

Creating an Android Image Slider Control for your App (Part 1 of 3)

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 […]

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.

Joel Garcia
Joel Garcia

Joel Garcia has been building AllCode since 2015. He’s an innovative, hands-on executive with a proven record of designing, developing, and operating Software-as-a-Service (SaaS), mobile, and desktop solutions. Joel has expertise in HealthTech, VoIP, and cloud-based solutions. Joel has experience scaling multiple start-ups for successful exits to IMS Health and Golden Gate Capital, as well as working at mature, industry-leading software companies. He’s held executive engineering positions in San Francisco at TidalWave, LittleCast, Self Health Network, LiveVox acquired by Golden Gate Capital, and Med-Vantage acquired by IMS Health.

Related Articles

Navigating AWS Complexity

Navigating AWS Complexity

Amazon’s Web Services is a very complex platform. Streamlining and optimizing production workflows can be challenging for inexperienced users. However, the benefit of learning grants options for better efficiency, reliability, security, and cost-effectiveness for operations run on AWS.

While complexity can be difficult to navigate, it’s not impossible. With the right level of expertise, AWS complexity can be navigated with ease.

What is Amazon Managed Grafana?

What is Amazon Managed Grafana?

Grafana stands out as a widely embraced open-source analytics and visualization platform, celebrated for its versatility in handling diverse data sources and delivering compelling dashboards and graphs. Renowned for its user-friendly interface, Grafana simplifies the process of data interpretation and enhances the overall experience by providing interactive visualizations.

AWS and re:Invent 2023

AWS and re:Invent 2023

There are plenty of AWS enthusiasts around the world such as ourselves with ideas on how to apply the Cloud in new and innovative ways. It’s a keynote where these enthusiasts come together, network, and share innovations and new methodologies with the public. Even for people less familiar with AWS, it is a great place to get first-hand experience with the platform either unguided or with professional help to see what opportunities the platform has.

Download our 10-Step Cloud Migration ChecklistYou'll get direct access to our full-length guide on Google Docs. From here, you will be able to make a copy, download the content, and share it with your team.