Recipe 3.7 Displaying a List of Images

Android Versions
Level 1 and above
Permissions
None
Source Code to Download from Wrox.com
Gallery.zip

So far, all the recipes covered involve displaying mainly text and occasionally images. If you want to display a series of images to the user, you can make use of the Gallery. The Gallery is a view that shows items (such as images) in a center-locked, horizontal scrolling list. This recipe shows you how to use it.

Solution

For this recipe, assume you have some images stored in the res/drawable-mdpi folder of your project (see Figure 3-15).

Create an XML file named attrs.xml and store it in the res/values folder (see Figure 3-16).

The content of the attrs.xml looks like this:

<resources>
    <declare-styleable name="MyGallery">
        <attr name="android:galleryItemBackground" />
    </declare-styleable>   
</resources>

To use the Gallery, add the <Gallery> element in your UI, such as the activity_main.xml file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <Gallery android:id="@+id/gallery1" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <ImageView android:id="@+id/image1" ...

Get Android Application Development Cookbook: 93 Recipes for Building Winning Apps now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.