Friday, September 16, 2011

Android Grid View Gallery

XMl Layout:



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content">


<ImageView android:id="@+id/ImageView01"
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:layout_centerHorizontal="true" 
android:scaleType="fitXY"/>


<ScrollView android:id="@+id/ScrollView01"
android:layout_width="wrap_content" 
android:layout_below="@+id/ImageView01"
android:layout_height="fill_parent">


</ScrollView>


<Button android:id="@+id/Button01" 
android:layout_above="@id/ScrollView01"
android:layout_width="wrap_content" 
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" 


android:text="Close" />





Java Source Code:



package com.sai.grid;


import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;


public class gridview extends Activity {
//---the thumbnail images to display---
Integer[] imageIDs = {
R.drawable.a_1, R.drawable.a_2, R.drawable.a_3, R.drawable.a_4,
R.drawable.sample_2, R.drawable.sample_3,
         R.drawable.sample_4, R.drawable.sample_5,
         R.drawable.sample_6, R.drawable.sample_7,
         R.drawable.sample_0, R.drawable.sample_1,
         R.drawable.sample_2, R.drawable.sample_3,
         R.drawable.sample_4, R.drawable.sample_5,
         R.drawable.sample_6, R.drawable.sample_7,
         R.drawable.sample_0, R.drawable.sample_1,
         R.drawable.sample_2, R.drawable.sample_3,
         R.drawable.sample_4, R.drawable.sample_5,
         R.drawable.sample_6, R.drawable.sample_7
};
//---the actual images to display---
Integer[] imageLargeIDs = {
R.drawable.a_1, R.drawable.a_2, R.drawable.a_3, R.drawable.a_4,
R.drawable.sample_2, R.drawable.sample_3,
         R.drawable.sample_4, R.drawable.sample_5,
         R.drawable.sample_6, R.drawable.sample_7,
         R.drawable.sample_0, R.drawable.sample_1,
         R.drawable.sample_2, R.drawable.sample_3,
         R.drawable.sample_4, R.drawable.sample_5,
         R.drawable.sample_6, R.drawable.sample_7,
         R.drawable.sample_0, R.drawable.sample_1,
         R.drawable.sample_2, R.drawable.sample_3,
         R.drawable.sample_4, R.drawable.sample_5,
         R.drawable.sample_6, R.drawable.sample_7
};


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set up main content view
setContentView(R.layout.main);


GridView gridView = (GridView) findViewById(R.id.gridview);
gridView.setAdapter(new ImageAdapter(this));


gridView.setOnItemClickListener(new OnItemClickListener()
{
            public void onItemClick(AdapterView parent,
View v, int position, long id)
{
                final Dialog dialog = new Dialog(gridview.this);
dialog.setContentView(R.layout.layoutdialog);
dialog.setTitle("Gallery Review");
dialog.setCancelable(true);


//set up image view
ImageView img = (ImageView) dialog.findViewById(R.id.ImageView01);
img.setPadding(5, 5, 5, 5);




img.setImageResource(imageLargeIDs[position]);


//set up button
Button button = (Button) dialog.findViewById(R.id.Button01);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
        dialog.dismiss();
}
});
//now that the dialog is set up, it's time to show it
dialog.show();
}
});


}


public class ImageAdapter extends BaseAdapter
{
private Context context;


public ImageAdapter(Context c)
{
context = c;
}


//---returns the number of images---
public int getCount() {
return imageIDs.length;
}


//---returns the ID of an item---
public Object getItem(int position) {
return position;
}


public long getItemId(int position) {
return position;
}


//---returns an ImageView view---
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(80,80));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setPadding(5, 5, 5, 5);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(imageIDs[position]);
return imageView;
}
}
}




Click on any one of the Image.




No comments:

Post a Comment