Android Gallery, ImageView Example

Already we saw the simple Gallery example on our previous post Simple Gallery Example
Now we are going to see a simple gallery example of how to use gallery look like a photo album as like in our phone. That is, when we click the item in gallery, the corresponding image will display below in full size using imageview.

Create a attrs.xml file in res/values folder. This file is used to declare the style. ( Code in previous post - simple gallery example).


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
<Gallery xmlns:android=""
android:id="@+id/examplegallery" android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ImageView android:id="@+id/ImageView01"
android:layout_width="wrap_content" android:layout_height="wrap_content"/>

Java file

public class GalleryExample extends Activity {

private Gallery gallery;
private ImageView imgView;

private Integer[] Imgid = {
R.drawable.a_1, R.drawable.a_2, R.drawable.a_3, R.drawable.a_4, R.drawable.a_5, R.drawable.a_6, R.drawable.a_7

public void onCreate(Bundle savedInstanceState) {

imgView = (ImageView)findViewById(;

gallery = (Gallery) findViewById(;
gallery.setAdapter(new AddImgAdp(this));

gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {


public class AddImgAdp extends BaseAdapter {
int GalItemBg;
private Context cont;

public AddImgAdp(Context c) {
cont = c;
TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);

public int getCount() {
return Imgid.length;

public Object getItem(int position) {
return position;

public long getItemId(int position) {
return position;

public View getView(int position, View convertView, ViewGroup parent) {
ImageView imgView = new ImageView(cont);

imgView.setLayoutParams(new Gallery.LayoutParams(80, 70));

return imgView;


Download the source code here

Output looks like




Hi Malv, why you need to save

Hi Malv,
why you need to save image in res/drawable folder.
Better you can directly show image to gallery after you directly download image from web.

Look at this example. You can get the image from web. return as drawable.
You can directly set drawable images to gallery.

Jai, If you need to show only

If you need to show only 4 images, then you can use imageview instead of gallery.
Imageview has a setOnClickListener(). Using this method you can easily find which imageview was clicked.

Hi Rob, try this gallery =

Hi Rob,

try this

gallery = (Gallery) findViewById(;
gallery.setAdapter(new AddImgAdp(this));

gallery1 = (Gallery) findViewById(;
gallery1.setAdapter(new AddImgAdp(this));

different gallery but same adapter.

@yusuf, Thanks yusuf :) nice

Thanks yusuf :)
nice post on gallery using web images.

John, of-course! you can use

of-course! you can use it.

@Gregg, Please find the


Please find the logcat that ends with ( ).
that's the error line we need to find. Please paste that line in comment to find the error.

@Gregg, It


It throws


There is no way to get ClassNotFound exception if you have downloaded the above source code. Try to download one more time & try it.

Here is a download link

see this eg. to get the postion of the image.