Create image gallery view in android application example tutorial

Create simple photo gallery just like web applications in android app to display series of images using BaseAdapter.

Gallery view is the most common view to display list of images already uploaded inside android project on activity screen. Gallery view supports multiple images at a single time using BaseAdapter class. Android application developer can upload multiple images and call them directly into gallery view. Gallery view also supports horizontal small thumbnail scrolling. So here is the complete step by step tutorial for Create image gallery view in android application example tutorial.

android-project-download-code-button

Note: All the images needed to uploaded inside drawable-hdpi folder.

drawable hdpi folder

All the below seven images are used in this project so if you want to run this project without showing image missing errors, so download all the images from below and upload them inside drawable-hdpi folder.

How to Create image gallery view in android application example tutorial.

Code for MainActivity.java file.

 package com.androidgallery_android_examples.com;
import android.app.Activity;
import android.content.Context;
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.AdapterView.OnItemClickListener;
import android.widget.ImageView.ScaleType;

public class MainActivity extends Activity {
 @SuppressWarnings("deprecation")
 Gallery Imagegallery;
 Integer[] GalleryImagesList = 
 {
 R.drawable.one,
 R.drawable.two,
 R.drawable.three,
 R.drawable.four,
 R.drawable.five,
 R.drawable.six,
 R.drawable.seven
 };
 ImageView imgGalleryImage;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) 
 {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 imgGalleryImage = (ImageView)findViewById(R.id.imgGalleryImage);
 imgGalleryImage.setImageResource(R.drawable.one);
 
 Imagegallery = (Gallery)findViewById(R.id.gallery);
 Imagegallery.setAdapter(new ImageAdapter(this));
 Imagegallery.setOnItemClickListener(new OnItemClickListener()
 {
 @Override
 public void onItemClick(AdapterView<?> parent, View view,
 int position, long id) 
 {
 imgGalleryImage.setImageResource(GalleryImagesList[position]);
 }
 });
 }
 
 private class ImageAdapter extends BaseAdapter
 {
 Context context;
 public ImageAdapter(Context context) 
 {
 this.context = context;
 }
 @Override
 public int getCount() 
 {
 return GalleryImagesList.length;
 }

 @Override
 public Object getItem(int position) 
 {
 return GalleryImagesList[position];
 }

 @Override
 public long getItemId(int position) 
 {
 return position;
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) 
 {
 ImageView imageView = new ImageView(this.context);
 imageView.setImageResource(GalleryImagesList[position]);
 imageView.setLayoutParams(new Gallery.LayoutParams(150, 200));
 imageView.setScaleType(ScaleType.FIT_XY);
 
 return imageView;
 }
 
 }
}

Code for activity_main.xml layout file.

 <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"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.androidgallery_android_examples.com.MainActivity"
 android:orientation="vertical" >
 
 <Gallery 
 android:id="@+id/gallery"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"/>

 <ImageView 
 android:id="@+id/imgGalleryImage"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"/>

</LinearLayout>

Screenshots:

Android Gallery view widget example tutorial with source code Download

Create image gallery view in android application example tutorial

android-gallery-screenshot-3

Click here to download Create image gallery view in android application example tutorial project.

One Comment

  1. Greet
    when scroll image not change to center lock always need click on image if any solution . thank you

Leave a Reply

Your email address will not be published. Required fields are marked *