Android Gallery View :

In your application if you want 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.

android Gallery View example 2

How to Create Android Gallery View:

  • Gallery is used to show Views in a horizontal list, and user will select a view , User selected view will be shown in center of the Horizontal list
  • The items of Gallery are get from an Adapter, just like ListView, in which ListView items are get from an Adapter.
  • We need to make an Adapter class that extends BaseAdapter class and override  getView() method.
  • getView() method known as automatically for all items of Gallery

Solution Stuff :

1. For Android Gallery View Example, assume you have some images stored in the res/drawable-mdpi folder of your project (see Figure).

android Gallery View example
2. Create an XML file named attrs.xml and store it in the res/values folder (see Figure).

android Gallery View example13. Add  this content to the attrs.xml file:

File: res/values/attrs.xml

4. Open “res/layout/activity_main.xml” file and put this xml code it into file:

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

File : res/layout/activity_main.xml

5. Open “MainActivity.java” file and add following JAVA code.

File : src/package-name/MainActivity.java

In Android Gallery View , you need create the ImageAdapter class which extends the BaseAdapter class.Which will bind to the Gallery view with a series of ImageView views. The BaseAdapter class will work as a bridge between an AdapterView and also the data source that feeds data into it.

For the ImageAdapter class, you implemented the following methods:

  •  getCount()
  •  getItem()
  •  getItemId()
  •  getView()

When a picture in the Gallery view is selected, the selected image’s position (0 for the first image, 1 for the second image, and so on) is displayed and the image is displayed in the ImageView.

 Output of  Android  Gallery View Example in emulator :

android Gallery View example 3

Download Android Gallery View example Source code :

 Note : This example (Project) is developed in Eclipse Version Kepler,tested on Android 4.4.2 (KitKat Version) and SDK build-tools 23.0

  1. hello there, first of all i would like to thank you for this tutorial. I have tried it and it work but something wrong somewhere my gallery view appeared to be too small and after scroll it will stuck at middle picture and not scrolling anymore. I have tried looking all over the internet somehow there’s so few i found and can’t fix it yet.

  2. Hello,
    thank you for the tutorial 🙂 I just have one question : what if all picture are not taken in the same orientation (landscape or not) ? Is there a way to make the thumbnails rotate depending on the orientation ?

    • R.K.Kanojia (Android App Developer)

      yes, you can do it ….take width and height of image, if height >width then its orientation vertical or height

Leave a Reply