Android Gallery view widget example tutorial with source code Download

Create simple gallery view in android application to display list of images inside it using Eclipse & Android studio.

Gallery view is used to display list of multiple images into android application with on one screen with horizontal scrollable view so whenever app developer clicks on any image it will automatically get large size in given imageview. So here is the complete step by step tutorial for Android Gallery view widget example tutorial with source code Download.

android-project-download-code-button

Note: Put all gallery images inside drawable-hdpi folder.

drawable hdpi folder

Download all 7 images from below and copy them inside drawable-hdpi folder.

Android Gallery view widget example tutorial with source code Download.

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:

Click here to download Android Gallery view widget example tutorial with source code Download

Leave a Reply

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