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.
Note: Put all gallery images inside 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.