Android Gallery View – List of Images

Gallery is used to show Views in a horizontal list, and user will select a view , User selected view will be shown in center of the Horizontal list

Step : 1 – res/values/attrs.xml

<resources>
<declare-styleable name="MyGallery">
<attr name="android:galleryItemBackground" />
</declare-styleable>
</resources>

Step : 2 – res/layout/activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="{relativePackage}.${activityClass}"
    android:orientation="vertical" >
 
    <Gallery
        android:id="@+id/gallery1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
 
    <ImageView
        android:id="@+id/image1"
        android:layout_width="215dp"
        android:layout_height="315dp"
        android:layout_gravity="center_horizontal"
        android:background="#cfcfcf"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:src="@drawable/pic1" />
</LinearLayout>

Step : 3 – src/package-name/MainActivity.java

package androidinterview.com.androidgalleryview;
 
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.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
 
@SuppressWarnings("deprecation")
public class MainActivity extends Activity {
 
 //the images to display
 Integer[] imageIDs = {
 R.drawable.pic1,
 R.drawable.pic2,
 R.drawable.pic3,
 R.drawable.pic4,
 R.drawable.pic5,
 R.drawable.pic6,
 R.drawable.pic7
 };
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 // Note that Gallery view is deprecated in Android 4.1---
 Gallery gallery = (Gallery) findViewById(R.id.gallery1);
 gallery.setAdapter(new ImageAdapter(this));
 gallery.setOnItemClickListener(new OnItemClickListener() {
 public void onItemClick(AdapterView<?> parent, View v, int position,long id)
 {
 Toast.makeText(getBaseContext(),"pic" + (position + 1) + " selected",
 Toast.LENGTH_SHORT).show();
 // display the images selected
 ImageView imageView = (ImageView) findViewById(R.id.image1);
 imageView.setImageResource(imageIDs[position]);
 }
 });
 }
 
 public class ImageAdapter extends BaseAdapter {
 private Context context;
 private int itemBackground;
 public ImageAdapter(Context c)
 {
 context = c;
 // sets a grey background; wraps around the images
 TypedArray a =obtainStyledAttributes(R.styleable.MyGallery);
 itemBackground = a.getResourceId(R.styleable.MyGallery_android_galleryItemBackground, 0);
 a.recycle();
 }
 // returns the number of images
 public int getCount() {
 return imageIDs.length;
 }
 // returns the ID of an item
 public Object getItem(int position) {
 return position;
 }
 // returns the ID of an item
 public long getItemId(int position) {
 return position;
 }
 // returns an ImageView view
 public View getView(int position, View convertView, ViewGroup parent) {
 ImageView imageView = new ImageView(context);
 imageView.setImageResource(imageIDs[position]);
 imageView.setLayoutParams(new Gallery.LayoutParams(100, 100));
 imageView.setBackgroundResource(itemBackground);
 return imageView;
 }
 }
}

Enjoy Code.