GalleryView in Android

GalleryView in Android

Here is the coding for, how to set the images in the galleryview. The images in the galleryview can be scrollable and easily able to switch another picture, First, The XML file for the galleryview.

GalleryView

MainActivity.xml  
//Here you have to set the view for image and gallery.

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Images" />
    <Gallery
        android:id="@+id/gallery1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <ImageView
        android:id="@+id/image1"
        android:layout_width="320px"
        android:layout_height="250px"
        android:scaleType="fitXY" />
</LinearLayout>
//Then you have to set the style for the galleryView.
res/values/img.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Gallery1">
<attr name="android:galleryItemBackground"/>
</declare-styleable>
</resources>
//finally have to add a class for the galleryview.
MainActivity.java
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;
 
public class MainActivity extends Activity {
    
    //---the images to display---
    Integer[] imageIDs = {
    R.drawable.apps,
    R.drawable.camera,
    R.drawable.computer,
    R.drawable.gaming,
    R.drawable.mobile,
    R.drawable.social,
        };
    ImageView img_view;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Gallery gallery = (Gallery) findViewById(R.id.gallery1);
        img_view=(ImageView)findViewById(R.id.image1);
//set the images in the galleryView.
        gallery.setAdapter(new ImageAdapter(this));
        gallery.setOnItemClickListener(new OnItemClickListener()
        {
        public void onItemClick(AdapterView<?> parent, View v,
        int position, long id)
        {
        img_view.setBackgroundResource(imageIDs[position]);
        Toast.makeText(getBaseContext(),
        "pic" + (position + 1) + "selected",
        Toast.LENGTH_SHORT).show();
        }
        });
    }
    
    public class ImageAdapter extends BaseAdapter
    {
      private Context context;
      private int itemBackground;
       public ImageAdapter(Context c)
       {
          context = c;
          //---setting the style---
          TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
          itemBackground = a.getResourceId(
          R.styleable.Gallery1_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.setScaleType(ImageView.ScaleType.FIT_XY);
    imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));
    imageView.setBackgroundResource(itemBackground);
  
    return imageView;
    }
    }  
}

Download

Leave a Reply

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

Back to Top