Gallery

Android’s Gallery tutorial. A Gallery usually displays images, but it can also display other types of views. For example, the ApiDemos People example displays TextViews. A Gallery uses the same Adapter as the Spinner we saw here and the GridView we saw here.

Source code in Gallery.zip

  1. GalleryActivity.java
  2. main.xml: one gallery element.
  3. attrs.xml: the value of the name is galleryItemBackground
  4. ab4241.png
  5. dm1969.png
  6. fg599.png
  7. gv490.png
  8. hcl211.png
  9. jr3136.png
  10. lk1332.png
  11. nrp243.png
  12. ri2027.png
  13. shl249.png
  14. wh521.jpg

android-sdks/platforms/android-14/data/res/drawable/gallery_item_background.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2008 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<selector xmlns:android="http://schemas.android.com/apk/res/android">


    <!-- When the window does not have focus. -->

    <item android:drawable="@drawable/gallery_selected_default"
        android:state_selected="true"
        android:state_window_focused="false"
        />

    <item android:drawable="@drawable/gallery_unselected_default"
        android:state_selected="false"
        android:state_window_focused="false"
        />


    <!-- When the window does have focus. -->

    <item android:drawable="@drawable/gallery_selected_pressed"
        android:state_selected="true"
        android:state_pressed="true"
        />

    <item android:drawable="@drawable/gallery_selected_focused"
        android:state_selected="true"
        android:state_focused="true"
        />

    <item android:drawable="@drawable/gallery_selected_default"
        android:state_selected="true"
        />

    <item android:drawable="@drawable/gallery_unselected_pressed"
        android:state_selected="false"
        android:state_pressed="true"
        />

    <item android:drawable="@drawable/gallery_unselected_default"
        />

</selector>

ApiDemos/Views/Gallery

The Photos demo displays a toast for a short press and a context menu for a long press on a photo. The People demo reads the people from the Contacts app. See here for a Cursor that reads from an Sqlite database.

Excerpt from the onCreate method of Gallery2.java.

	//Which columns do you want to read from the Contacts app?
	//The _ID column is required.

	private static final String[] CONTACT_PROJECTION = new String[] {
		Contacts._ID,
		Contacts.DISPLAY_NAME
	};

	//nulls to get all the people, without bothering to sort.

	ContentResolver contentResolver = getContentResolver();
	Cursor c = contentResolver.query(Contacts.CONTENT_URI, CONTACT_PROJECTION, null, null, null);
	startManagingCursor(c);	//deprecated

	/*
	The two following arrays have to be the same length.
	Each string in the DISPLAY_NAME column will be displayed in the
	TextView whose id is android:id="@android:id/text1"
	*/

	SpinnerAdapter adapter = new SimpleCursorAdapter(this,
		android.R.layout.simple_gallery_item, //.xml file in flash memory
                c,
                new String[] {Contacts.DISPLAY_NAME},
                new int[] {android.R.id.text1}
	);

        Gallery g = (Gallery)findViewById(R.id.gallery);
        g.setAdapter(adapter);

Here is /Users/nyuuser/android-sdks/platforms/android-14/data/res/layout/simple_gallery_item.xml.

<?xml version="1.0" encoding="utf-8"?>
<!--
/* //device/apps/common/res/layout/simple_gallery_item.xml
**
** Copyright 2007, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
**     http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
-->

<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="?android:attr/textColorPrimaryDisableOnly"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
	android:maxLines="1" />
  1. 1. Photos
    1. Gallery1.java
    2. gallery_1.xml
    3. attrs.xml
    4. strings.xml
    5. gallery_photo_1.jpg
    6. gallery_photo_2.jpg
    7. gallery_photo_3.jpg
    8. gallery_photo_4.jpg
    9. gallery_photo_5.jpg
    10. gallery_photo_6.jpg
    11. gallery_photo_7.jpg
    12. gallery_photo_8.jpg
  2. 2. People
    1. Gallery2.java
    2. gallery_2.xml
    3. strings.xml

Things to try

  1. Set an OnScrollListener by calling setOnScrollListener as in ApiDemos/Views/Lists/9. Array (Overlay).