Are you getting the most out of your AWS investment? Get your free AWS Well-Architected Assessment.

2021 Fillmore Street #1128

}

24/7 solutions

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

What is AWS Pinpoint?

What is AWS Pinpoint?

Along with other marketing tools, AWS Pinpoint is a solution to better allow for multi-channel marketing. It is designed to work with current channels of communication and offers flexibility in its application. It is everything needed for campaigns, tracking customer interaction, and utilizing data to improve marketing efforts.

Traditional IT vs. AWS – How Small Businesses can Benefit

Traditional IT vs. AWS – How Small Businesses can Benefit

AWS solutions can accomplish a variety of problems and tasks including IT needs. Even smaller businesses that have a more limited scope that their businesses cover can look to find some way to upgrade their business operations through what Amazon has to offer. Though it may be intimidating and difficult to adapt to, there is more than enough reason to adopt AWS.

AWS Think Big with Small Business Program’s Competitive Edge

AWS Think Big with Small Business Program’s Competitive Edge

The Amazon Cloud can help alleviate most issues involving transitioning the cloud. Businesses of any scope can hope to build solutions that are scalable and adaptable to their industry of work. Smaller or minority-owned businesses may still struggle to stand out among bigger companies or make an AWS environment as efficient as possible with fewer funds to spend. To alleviate these burdens, Amazon has the Think Big With Small Business program available through their Public Sector partnership program.