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
TextView
s.
A Gallery uses the same
Adapter
as the
Spinner
we saw
here
and the
GridView
we saw
here.
GalleryActivity.java
main.xml
:
one
gallery
element.
attrs.xml
:
the value of the name is
galleryItemBackground
ab4241.png
dm1969.png
fg599.png
gv490.png
hcl211.png
jr3136.png
lk1332.png
nrp243.png
ri2027.png
shl249.png
wh521.jpg
<?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>
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. Photos
2. People
OnScrollListener
by calling
setOnScrollListener
as in
ApiDemos/Views/Lists/9. Array (Overlay).