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="http://schemas.android.com/apk/res/android"
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
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 {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Reference the Gallery view
Gallery g = (Gallery) findViewById(R.id.gallery);
g.setSpacing(2);
// 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.setImageResource(mImageIds[position]);
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setLayoutParams(new Gallery.LayoutParams(136, 88));
return i;
}
private Context mContext;
private Integer[] mImageIds = {
R.drawable.gallery_photo_1,
R.drawable.gallery_photo_2,
R.drawable.gallery_photo_3,
R.drawable.gallery_photo_4,
R.drawable.gallery_photo_5,
R.drawable.gallery_photo_6,
R.drawable.gallery_photo_7,
R.drawable.gallery_photo_8
};
}
}
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.
Thanks for shanirg. What a pleasure to read!
Thanks for shanirg
but can u ask me haw i can dawnload the gallery_photo from internet
You can check this post, Android Infinite Loop Gallery
. Download the source code and you can find out the example how to download the image from internet
Thanks. But if you want do your visitors a favor add some example images.
Thank, Tobias. That’s a good idea.
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?
Hello fjr619,
You can extend the android gallery and override the onfling function. Here is the snapshot of the orerride onfling function:
@Override
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.
Thanks
Hello James,
can you make separate file for that example? u can send me that file into my email. frank.jr.619@gmail.com thanks for your help
Hi. How would I detect if an image was clicked on this one? Thanks 🙂
hi,,,thanks for sharing but image s not zooming ,,,plz can i get that code ?
Thank You!!!!
Best Gallary Example on the NET!!!
hello sir,
can tell me how to implement the code for zoom for an view pager
Hi,
I will post a tutorial for it in future.
Thanks
really nice