Saturday 19 January 2013

GalleyView with ImageView in android..

GalleyView with ImageView in android..

Create project name as Gallery package com.bskTech.com.bskTech.Gallery

you need images this images copy and past in res/draweble-midp folder
or add your favorite images named as img2.jpg, img3.jpg, img4.jpg




create activity for View in res/Layout/main.xml edit


<?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="@string/hello" />
<Gallery android:layout_height="wrap_content"
android:layout_width="fill_parent" android:id="@+id/gallery1"></Gallery>
<ImageView android:scaleType="fitXY" android:layout_height="300px"
android:layout_width="320px" android:id="@+id/imageView1"></ImageView>
</LinearLayout>


we created first GalleryView to view the all images in that 
and if u click on any image this image will display in image view that's why we put down there imageView


now open the src/com.bskTech.Gallery/Gallery.java file add code


package com.bskTech.Gallery;

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.ImageView;
import android.widget.Toast;

public class Gallery extends Activity {

Integer[] imageIDs={R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4};
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        android.widget.Gallery gallery=(android.widget.Gallery) findViewById(R.id.gallery1);
        gallery.setAdapter(new ImageAdapter(this));
        gallery.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "pic"+(position+1)+" selected", Toast.LENGTH_SHORT).show();

//dispaly the images u selected
ImageView imageView=(ImageView) findViewById(R.id.imageView1);
imageView.setImageResource(imageIDs[position]);
}
});
    }
    
    public class ImageAdapter extends BaseAdapter{
    private Context context;
    private int itemBackground;

public ImageAdapter(Context c) {
// TODO Auto-generated constructor stub
context=c;
TypedArray a=obtainStyledAttributes(R.styleable.Gallery1);
itemBackground=a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}

public int getCount() {
// TODO Auto-generated method stub
return imageIDs.length;
}

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

public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imageView=new ImageView(context);
imageView.setImageResource(imageIDs[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new android.widget.Gallery.LayoutParams(150,120));
imageView.setBackgroundResource(itemBackground);
return imageView;
}
   
    }
    
}

first we created one array named imageIDs we stored reference ids of images in that array.
then we set adapter to the gallaryView using setAdapter(new ImageAdapter(this)); method
ImageAdapter is the inner class extended by baseAdapter where some already implemented methods.

now set the on click listner to the galleryView
using setOnItemClickListener() method. add pass the OnItemClickListener() which is uesd to identify on  which  item u clicked
when u click on the image we set the image to the image view using
setImageResource(imageIDs[position]);  method.. position is parameter in onitemclick listener method..

getView() method in the ImageAdapter set the view to the gallery view

click run...