Creating Image Gallery

The project describes how to implement gallery for your application.

Underlying Algorithm:

Basic description of algorithm in step by step form:

1.) Create a Project GalleryExample

2.) Open and insert following in main.xml:

 

 

 

3.) Create,Open and insert following in your res/values/attrs.xml:

4.) Add some image in your drawable folder.

5.) Run the application.

Steps to Create:

1.) Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name i.e. GalleryExample. Enter following information:

Project name: GalleryExample
Build Target: Android 2.3.3
Application name: GalleryExample
Package name: org.example.GalleryExample
Create Activity: GalleryExample

On Clicking Finish GalleryExample code structure is generated with the necessary Android Packages being imported along with GalleryExample.java. GalleryExample class will look like following:

 

 

 

Output – The final output:

Leave a Comment:

19 comments
carlos says April 3, 2011

how do you create the res/values/attrs.xml folder??

Reply
    Sushant says April 7, 2011

    res/values folder is automatically created when you create project. And you can create a new android xml there with name attrs.xml.

    Reply
Wilsharo Scott says May 17, 2011

How do you get it to display the actual image, instead of a toast message?

Reply
    Sushant says May 18, 2011

    Just replace following in onItemClick() instead of Toast message:

    [code lang="java"]
    ImageView imageview = (ImageView) findViewById(R.id.ImageView01);
    imageview.setImageResource(mImageIds[position]);
    [/code]

    and add a ImageView in your main.xml as below:

    [code lang="xml"]
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&quot;
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Gallery android:id="@+id/Gallery01" android:layout_width="fill_parent"
    android:layout_height="wrap_content"></Gallery>
    <ImageView android:id="@+id/ImageView01"
    android:layout_gravity="center_vertical|center_horizontal"
    android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
    </LinearLayout>
    [/code]

    Also don’t forget to define resource array “mImageIds” in your activity instead of in imageadapter class.

    Reply
KEVIN says July 11, 2011

TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);
This line gives me an error, saying it wants to create an undeclared method..?
I’ve seen a lot of tutorials but all of the code is the same and it still won’t work for me.
Any idea why? thanks!

Reply
Sushant says July 12, 2011

Hello Kevin,

Either you havn’t imported “import android.content.res.TypedArray;” or you haven’t declared styleable resource in your attrs.xml file as below.

[code lang="xml"]
<resources>
<declare-styleable name="HelloGallery">
<attr name="android:galleryItemBackground" />
</declare-styleable>
</resources>
[/code]

These two are possible causes of the error your are getting. Please try to clean your project and recompile after applying above suggestions.

Thanks.

Reply
Mike says December 5, 2011

There are 400 examples of this gallery on the net including to original a Developer.Android.com called “Hello Image Gallery”, Hello!!! If you’re going to put stuff up on the Net, why not extend on it and not clutter it!

Reply
Sharma says April 11, 2012

is this applicable in android above than 2.3.3 ?
kindly reply

Reply
Roseman says June 1, 2012

Thanks for the code. It nearly works but I can’t work out how to:

‘Define resource array “mImageIds” in your activity instead of in imageadapter class.’

Could you should me how to do that exactly – the alterations required.

Thanks a lot.

Reply
ranaomair says July 20, 2012

Would you please upload the code, as its not working, and there are many issues, in this.
you might didn’t mention, or you forgot, to write here.
sorry to say ,but its not fully described.

Reply
ranaomair says July 20, 2012

Would you please upload the code, as its not working, and there are many issues, in this.
you might didn’t mention, or you forgot, to write here.
sorry to say ,but its not fully described.

Reply
Arwa says August 29, 2012

The following classes could not be found:
– Gallery (Change to android.widget.Gallery, Fix Build Path, Edit XML)
it is giving me this error in the layout ! something wrong with inserting gallery.

Reply
Paul says October 17, 2012

It seems to work as long as i use the same image over and over. But if i reference different images in mImageIds I get an OutOfMemory error. Even in your screen shots you are showing the same image over and over.

Reply
neema says December 15, 2012

tnx, it was incredible.
edumobile has taught me so much.

Reply
Kmailia says December 20, 2012

hi,

how to avoid centering the select one.
just scrolling without focuses on item.

Thanks,

Reply
Suleyman says March 10, 2013

Hello everyone,

Thank you for helping, all of the seem okay except R, all the errors come from R can’t be resolved to a variable.

Could you help me in this error.

Thank you.

Reply
supercool says July 24, 2013

does not work
🙁

MfG
supercool3

Reply
Smiley says July 29, 2013

I’m a beginner in learning Android and i tried using the code provided for creating the image Gallery, but then there are errors whereby it keeps telling me that “the type Gallery depreciated”. What should i do? or is there any other way to solve it??
Thank you!

Reply
Fix Android Imageview Setimageresource Outofmemory Error Windows XP, Vista, 7, 8 [Solved] says December 15, 2014

[…] Creating image gallery | Android Programming Tutorials – Just replace following in onItemClick() instead of Toast message: ImageView imageview = (ImageView) findViewById(R.id.ImageView01); imageview …… […]

Reply
Add Your Reply