In Android the Gallery control is a selection control that displays items in a horizontal gallery. the items in the gallery appear beside each other. they can appear separated by a pre-defined space.
remember that there is a sample demo application for the gallery to download at the end of the postwe can use the gallery to display String items using a simple
ArrayAdapter.so lets see how to create a gallery that displays the word "Hello" in several languages:
the layout:
01 | <?xml version="1.0" encoding="utf-8"?> |
02 | <LinearLayout >="http://schemas.android.com/apk/res/android" |
03 | android:orientation="vertical" |
04 | android:layout_width="fill_parent" |
05 | android:layout_height="fill_parent" |
08 | android:layout_width="fill_parent" |
09 | android:layout_height="wrap_content" |
10 | android:text="Gallery Demo" |
13 | android:id="@+id/gallery" |
14 | android:layout_width="fill_parent" |
15 | android:layout_height="wrap_content" |
16 | android:gravity="center_horizontal" |
17 | android:spacing="100px" |
and in the
OnCreate method
02 | public void onCreate(Bundle savedInstanceState) { |
03 | super.onCreate(savedInstanceState); |
04 | setContentView(R.layout.main); |
05 | gallery=(Gallery)findViewById(R.id.gallery); |
06 | //String array holding the values |
07 | String [] text=new String[]{"Hello","Hi","Alloha","Bonjour","Hallo","¡Hola"}; |
08 | //Array adapter to display our values in the gallery control |
09 | ArrayAdapter<string> arr=new ArrayAdapter<string>(this, android.R.layout.simple_gallery_item, text); |
10 | gallery.setAdapter(arr); |
the gallery will be like this
we can increse the spacing between the items by increasing the value of
android:spacing property.
we can display a scroll bar to indicate the position of the current selected item in the gallery like this:
02 | android:id="@+id/gallery" |
03 | android:layout_width="fill_parent" |
04 | android:layout_height="wrap_content" |
05 | android:gravity="center_horizontal" |
06 | android:spacing="100px" |
07 | android:scrollbars="horizontal" |
08 | android:scrollbarFadeDuration="0" |
09 | android:scrollX="100px" |
setting the
android:scrollbarFadeDuration="0" makes the scroll bar always visible.
The
android:scrollX property defines the initial scroll offset of the scroll bar which is the initial distance that the gallery is scrolled for.
Handling Gallery Eventssince the gallery is a selction Control (a adapter view) so it can register a
OnItemSelectedListener to handle the selection of items within the gallery.
01 | final String [] text=new String[]{"Hello","Hi","Alloha","Bonjour","Hallo","¡Hola"}; |
02 | gallery.setOnItemSelectedListener(new OnItemSelectedListener() { |
05 | public void onItemSelected(AdapterView parent, View view, |
06 | int position, long id) { |
07 | // TODO Auto-generated method stub |
08 | TextView txt=(TextView)findViewById(R.id.txt); |
09 | txt.setText(text[position].toString()); |
13 | public void onNothingSelected(AdapterView parent) { |
14 | // TODO Auto-generated method stub |
now the final step is to add two navigation buttons: Next and Previous to navigate throught the items in the gallery.
the layout is gonna be like this:
01 | <?xml version="1.0" encoding="utf-8"?> |
02 | <LinearLayout >="http://schemas.android.com/apk/res/android" |
03 | android:orientation="vertical" |
04 | android:layout_width="fill_parent" |
05 | android:layout_height="fill_parent" |
08 | android:layout_width="fill_parent" |
09 | android:layout_height="wrap_content" |
10 | android:text="Gallery Demo" |
14 | android:id="@+id/gallery" |
15 | android:layout_width="fill_parent" |
16 | android:layout_height="wrap_content" |
17 | android:gravity="center_horizontal" |
18 | android:spacing="100px" |
19 | android:scrollbars="horizontal" |
20 | android:scrollbarFadeDuration="0" |
21 | android:scrollX="100px" |
24 | android:layout_width="fill_parent" |
25 | android:layout_height="wrap_content" |
26 | android:orientation="horizontal" |
27 | android:layout_marginTop="5px" |
30 | android:text="Previous" |
31 | android:layout_width="wrap_content" |
32 | android:layout_height="wrap_content" |
33 | android:id="@+id/btnPrev" |
34 | android:onClick="onClick" |
38 | android:layout_width="wrap_content" |
39 | android:layout_height="wrap_content" |
40 | android:id="@+id/btnNext" |
41 | android:onClick="onClick" |

now in order to keep track of the index of the currently selected item we need to define two variables
1 | //Variable to store the number of items in the gallery |
and the navigation buttons click handlers:
02 | public void onClick(View v) { |
03 | // TODO Auto-generated method stub |
09 | //if reached the end of the gallery, then start from the first item |
10 | if(CurrentIndex>ItemsInGallery-1) |
12 | gallery.setSelection(CurrentIndex,true); |
13 | txt.setText(String.valueOf(CurrentIndex)); |
17 | CurrentIndex=CurrentIndex-1; |
18 | //If reached the first item, then return to the last item in the gallery |
20 | CurrentIndex=ItemsInGallery-1; |
21 | gallery.setSelection(CurrentIndex,true); |
22 | txt.setText(String.valueOf(CurrentIndex)); |
you can download a sample program from here