Android Gallery Example

In android, we can show multiple image in gallery view. Here is a android gallery example that will explain how to show the images in gallery view.
Example for Android Gallery :-
Create a attrs.xml file in res/values folder. This file is used to declare the style.
Your attrs.xml looks like

[sourcecode language="xml"]
<?xml version="1.0" encoding="utf-8"?>

<resources>
<declare-styleable name="GalleryTheme">
<attr name="android:galleryItemBackground" />
</declare-styleable>
</resources>
[/sourcecode]

Now edit your main.xml in res/layout folder

[sourcecode language="xml"]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android" >
<Gallery xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/examplegallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
[/sourcecode]

Now your java code should looks like

[sourcecode language="java"]
package com.pack.gallery;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class GalleryExample extends Activity {

private Gallery gallery;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

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

gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
// Displaying the position when the gallery item in clicked
Toast.makeText(GalleryExample.this, "Position=" + position, Toast.LENGTH_SHORT).show();
}
});

}

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

// Adding images.
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 AddImgAdp(Context c) {
cont = c;
TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
typArray.recycle();
}

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.setImageResource(Imgid[position]);
// Fixing width & height for image to display
imgView.setLayoutParams(new Gallery.LayoutParams(80, 70));
imgView.setScaleType(ImageView.ScaleType.FIT_XY);
imgView.setBackgroundResource(GalItemBg);

return imgView;
}
}

}
[/sourcecode]

The output will looks like

Also See the Latest Android Gallery,Imageview Example

Comments

Hi Ron, Have you used both

Hi Ron,
Have you used both attrs.xml & main.xml ?

Hi Ron, Here we are using

Hi Ron,
Here we are using attrs.xml for Gallery Style.
See the below code (48th and 49th Line)
TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
without attrs.xml we can't get the border and focus for gallery.

First of all Thanks for your

First of all Thanks for your comments :)
we can use url instead of direct images.
Now i'm not having that program. I will give some ideas about that in couple of days. :)

:)

:)

Hi Ron, Please look at this

Hi Ron,
Please look at this below link http://www.androidpeople.com/android-load-image-f...
This may help you. I think this will satisfy your needs. :)

Hi Ponzaro, When we are

Hi Ponzaro,
When we are clicking the gallery item image, it is displaying the position in toast. In the gallery onclicklistener() action place remove the toast and create a new imageview and display the image in full size.
questions ?

OK ponzaro. But not today. I

OK ponzaro. But not today. I will start doing in few days.
And I will post the article in site. Keep Seeing..

Hi Epoch, I don't know about

Hi Epoch,
I don't know about this exactly.
I think that when we are not using the theme.
The setUnselectedAlpha() method won't be enable when the theme is not used.
I will refer one more time about the gallery & then i will respond you clearly.

Hi Av, Before posting here i

Hi Av,
Before posting here i will test it, after that only i will post here.
we need to add image in res/drawable folder. So that we can call it in Imgid[].
Questions ?

ok ponzaro. Within couple of

ok ponzaro. Within couple of days i will do it.

Hi ponzaro, As i mentioned

Hi ponzaro,
As i mentioned today morning i wrote a post about the gallery with imageview.
Please see the below link http://www.androidpeople.com/android-gallery-imag...
I think this will satisfy your needs in gallery.
When you click the gallery item according to that correspondence image will be displayed below with full size.
Questions ?

Thanks for your comments

Thanks for your comments Ragna :)

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 download image from web.
http://www.androidpeople.com/android-load-image-f...

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

@malv, You can look at this

@malv,

You can look at this example

http://www.yusufk.za.net/?p=538

this explains how to show web image in gallery

see this comment also

http://www.androidpeople.com/android-gallery-imag...

igorkh, I think Gallery will

igorkh,
I think Gallery will start in center position only.