Android Gallery Example: Widget Gallery

On May 5, 2011, in Android, by James Liu

Android provides a widget gallery by default. Widget gallery is the basic widget inside android. I also provide a more complicated gallery in my previous post: Android Infinite Loop Gallery. Using this gallery, an android programmer can create a picture gallery, or a album in a 5 minutes. Here, I will give you an example to show how easily to create a gallery in android. And You can also download the example, and source code to try.

The class of the basic gallery is android.widget.Gallery; In this example, I will show 8 pictures in the android gallery. So let’s see the layout file main.xml first.

<?xml version="1.0" encoding="utf-8"?>
<gallery xmlns:android=""

As we show in the above example, we just simply put a widget gallery inside layout, and assign the id as “gallery”, which we will use in Activity class. Next code snap is the Activity class:

public class GalleryLoop extends Activity {
    public void onCreate(Bundle savedInstanceState) {

        // Reference the Gallery view
        Gallery g = (Gallery) findViewById(;

        // Set the adapter to our custom adapter (below)
        g.setAdapter(new ImageAdapter(this));

    public class ImageAdapter extends BaseAdapter {

        public ImageAdapter(Context c) {
            mContext = c;

        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.setLayoutParams(new Gallery.LayoutParams(136, 88));
            return i;

        private Context mContext;

        private Integer[] mImageIds = {

In onCreate function, we find the gallery by id “gallery”, and assign a ImageAdapter, which is customized BaseAdapter. The most important function in ImageAdapter is getView. Everytime, when we scroll the gallery, it will call this function to retrieve a image to show.

Download Source Code

Tagged with:  

14 Responses to “Android Gallery Example: Widget Gallery”

  1. Kamren says:

    Thanks for shanirg. What a pleasure to read!

  2. khaled says:

    Thanks for shanirg
    but can u ask me haw i can dawnload the gallery_photo from internet

  3. Tobias says:

    Thanks. But if you want do your visitors a favor add some example images.

  4. fjr619 says:

    how to make that gallery not loop? i mean when we swipe that gallery, just move to next picture. i mean your code now when i swipe that gallery it not move to next picture, i want just move to next picture, what must i change it?

    • James says:

      Hello fjr619,

      You can extend the android gallery and override the onfling function. Here is the snapshot of the orerride onfling function:

      public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
      return super.onFling(e1, e2, 0, velocityY);

      if you still have problem, I can write a seperate example for you. I think it will be very helpful.


  5. Jeraldo says:

    Hi. How would I detect if an image was clicked on this one? Thanks 🙂

  6. kushi says:

    hi,,,thanks for sharing but image s not zooming ,,,plz can i get that code ?

  7. androw says:

    Thank You!!!!
    Best Gallary Example on the NET!!!

  8. Rajashri says:

    hello sir,

    can tell me how to implement the code for zoom for an view pager

  9. android says:

    really nice

Leave a Reply

WordPress Blog