Android Gallery Tutorial working example

When you follow the Android Gallery Tutorial on the Android dev site using Eclipse and platform 2.2, you may notice that the code will not run. Here’s the working example.

The tutorial asks you to put all Java code into one file, however, when I tried this, Eclipse complained about this set up, so I ended splitting the code into two files. No big deal but good to know.

So here are the contents of HelloGallery.java:

package com.HelloGallery;

import android.app.Activity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import com.HelloGallery.ImageAdapter;
import android.view.View;



public class HelloGallery extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Gallery g = (Gallery) findViewById(R.id.gallery);
        g.setAdapter(new ImageAdapter(this));

        g.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                Toast.makeText(HelloGallery.this, "" + position, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

Wierdly, the OnItemClickListener kept complaining about a non-implemented method called onItemClick when the method was clearly implemented. Eclipse offered to implement it, so I went with that. I then replaced the names of the arguments and contents of the method generated by Eclipse with the ones from the tutorial.

Next, contents of ImageAdapter.java:

package com.HelloGallery;

import android.content.Context;
import android.content.res.TypedArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;


public class ImageAdapter extends BaseAdapter {
    int mGalleryItemBackground;
    private Context mContext;

    private Integer[] mImageIds = {
            R.drawable.sample_1,
            R.drawable.sample_2,
            R.drawable.sample_3,
            R.drawable.sample_4,
            R.drawable.sample_5,
            R.drawable.sample_6,
            R.drawable.sample_7
    };

    public ImageAdapter(Context c) {
        mContext = c;
       
        TypedArray a = c.obtainStyledAttributes(R.styleable.HelloGallery);
        mGalleryItemBackground = a.getResourceId(
                R.styleable.HelloGallery_android_galleryItemBackground, 0);
        a.recycle();
    }

    public int getCount() {
        return mImageIds.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 i = new ImageView(mContext);

        i.setImageResource(mImageIds[position]);
        i.setLayoutParams(new Gallery.LayoutParams(150, 100));
        i.setScaleType(ImageView.ScaleType.FIT_XY);
        i.setBackgroundResource(mGalleryItemBackground);

        return i;
    }
}

The problem here was that the reference to obtainStyledAttributes was nowhere to be found. Turns out this method – at least the version we need here – is implemented in the Context class, which is passed into the constructor as an argument named c. So, c.obtainStyledAttributes is what you need.

With these modifications the code should run.

7 Comments to “Android Gallery Tutorial working example”

  1. By sazid, January 24, 2011 @ 9:35 pm

    Hello Raivo,

    after a lot of googling my problem on Hellogallery is sovled..thanks to your solution…hope to receive more tutorials (with explanations) on android in ur blog…thanks again

    sazid

  2. By Andie Chan, January 27, 2011 @ 6:15 pm

    does this actually work? so i Load up abunch of image files and it will run in a gallery? is that how?

  3. By Raivo, January 29, 2011 @ 1:48 am

    @Andie Yup, it should. Though I must admit it doesn’t look that great, and this code won’t allow you to enlarge images.

  4. By Zeba, May 5, 2011 @ 2:14 am

    Hi,
    I’ve implemented Gallery in my application in the exact same manner mentioned above.
    My requirement is that I need to know which image is been currently shown in the gallery .
    I tried using the ‘position’ parameter’ in getView() for this purpose, but the values are very uncertain and not consecutive.
    Please help… How can i get the position of the currently displayed image?
    Thank You.

  5. By Raivo, May 7, 2011 @ 2:19 pm

    @Zeba: Have you tried getSelectedItemPosition() ?

    Here’s another solution

  6. By dbrooks7, August 11, 2011 @ 6:22 pm

    Hello, I tried this code and I get a “this application has stopped unexpectedly” error and have to ForceClose. What is the environment that you developed this on? I’m on Eclipse 3.7.0, SDK revision 12, and testing on SDK Platform Android 2.2 API 8.

  7. By Raivo, August 17, 2011 @ 9:08 pm

    I believe I used Eclipse Galileo or Helios. API 8 sounds about what I used as well. Sorry, it’s been a while.

RSS feed for comments on this post. TrackBack URI

Leave a Reply