Sunday, June 3, 2012

Android Gallery Example

Gallery is an internal element which can scroll horizontally and layout component of the currently selected child elements are positioned in the center of it.



STEP 1: Creating a new project from File->New->Android Project with Gallerydemo Activity Name

STEP 2: Copy Image files to res/drawable Folder

STEP 3: In res/layout/main.xm layout add ImageView and Gallery View as:
 
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <!-- ImageView for display image in background -->  
   <ImageView  
        android:id="@+id/imgview"  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"  
        android:src="@drawable/a01"  
         /> 
         
         <!-- Gallery To show images Gallery  -->  
        <Gallery xmlns:android="http://schemas.android.com/apk/res/android"  
        android:id="@+id/galleryview"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:spacing="5dp"  
        /> 

</FrameLayout >

STEP 4: In res/values add attrs.xml as:
 
<?xml version="1.0" encoding="utf-8"?>
<resources>
   <declare-styleable name="HelloGallery">  
        <attr name="android:galleryItemBackground"/>  
   </declare-styleable>
</resources>

STEP 5: In Gallerydemo Activity Create an ImageAdapter which extends BaseAdapter as:
package com.imrankhanandroid.Galleryexp;

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.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class Gallerydemo extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      //Remove title bar
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
      //Remove notification bar
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.main);
        
        final ImageView imagevew = (ImageView)findViewById(R.id.imgview);  
        Gallery gallryview = (Gallery)findViewById(R.id.galleryview); 
        gallryview.setAdapter(new ImageAdapter(this));
        gallryview.setOnItemClickListener(new OnItemClickListener() {

	@Override
	public void onItemClick(AdapterView parent,
            View view, int position,long id) {
		// TODO Auto-generated method stub
	        // The first few pictures show click 
		Toast.makeText(Gallerydemo.this, "" + position,  
		         Toast.LENGTH_LONG).show();  
		//Set the backgroundPart of theImageView
		imagevew.setImageResource(((ImageView) view).getId());
			}
		});
    }
    public class ImageAdapter extends BaseAdapter {

    	int mGalleryItemBackground;  
    	// Context object 
    	private Context mContext;  
    	// Picture array 
    	private Integer[] mImageIds ={
    			R.drawable.a01,R.drawable.a02,
    			R.drawable.a03,R.drawable.a04,
    			R.drawable.a05,R.drawable.a06,
    			R.drawable.a07,R.drawable.a08,
    			R.drawable.a09,R.drawable.a010,
    	};
    	// ImageAdapter Constructor 
    	public ImageAdapter(Context c) {
    	  this.mContext = c;
    	  //styleable resources
    	  TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);
    	  mGalleryItemBackground = a.getResourceId(
    		R.styleable.HelloGallery_android_galleryItemBackground,0);
    		a.recycle();
    	}
    	// Get  number of items 
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mImageIds.length;  
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}
		//Return  view 
		@Override
		public View getView(int position, View convertView, 
                   ViewGroup parent) {
			// TODO Auto-generated method stub
			ImageView iv = new ImageView(mContext); 
			iv.setImageResource(mImageIds[position]);
			iv.setId(mImageIds[position]);  
			iv.setLayoutParams(new Gallery.LayoutParams(120, 160));  
			iv.setScaleType(ImageView.ScaleType.FIT_XY);
			iv.setBackgroundResource(mGalleryItemBackground); 
			return iv;
		}
    }
    
}

Download Source Code: GalleryExample








1 comments:

Pawan said...
This comment has been removed by the author.

Post a Comment

 
Powered by Blogger