Android Gallery View Example Tutorial

Android Gallery is a View commonly used to display items in a horizontally scrolling list that locks the current selection at the center. In this tutorial we’ll display a horizontal list of images and when a user clicks an image, it will be displayed in the center of the screen.

Android Gallery View Overview

  • The items of Gallery are populated from an Adapter, similar to ListView, in which ListView items were populated from an Adapter
  • We need to create an Adapter class which extends BaseAdapter class and override getView() method
  • getView() method called automatically for all items of Gallery

The layout for the Gallery is defined as follows :

It belongs to android.widget.Gallery class. However this class is deprecated now.

Project Structure

android-gallery-view-project-227x450

Code

The layout of the MainActivity is given below:

main.xml

The android:src points to the first image from the left in the gallery.

The MainActivity.java is given below:

We need to create the GalleryImageAdapter class which extends the BaseAdapter class. This 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 GalleryImageAdapter class, following methods are implemented:

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

The GalleryImageAdapter class is given below:

The GIF below depict the output of the project. They display the ImageView with the image of the corresponding thumbnail from the GalleryView.
android gallery view example, android gallery example

Note: GalleryView is deprecated now. The alternatives include HorizontalScrollView and ViewPager from the support library. The best alternative way is to use ViewPager with an ImageView in its fragment layout.

This brings an end to this tutorial. You can download the final Android GalleryView Project from the below link:

By admin

Leave a Reply

%d bloggers like this: