Introduction

This article explains how to use images in a GridView and how to the GridView to another activity upon a click action.
GridView
It is a view group that displays items in a two-dimensional grid.
Before doing the code part first you need to learn the functionality of this application. In this application first, you need to use a GridView on which you want to show the images.
Second, you need to create an array that contains images.
To these images to the GridView you need to create an Adapter class that extends the baseadpter class.You can all these images to the GridView by calling the setAdapter method with the object of the Grid View and the Adapter class object as a parameter.
Step 1
Create an XML file with this:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <GridView xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/gridview1"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:numColumns="auto_fit"
  7. android:columnWidth="90dp"
  8. android:horizontalSpacing="5dp"
  9. android:verticalSpacing="10dp"
  10. android:gravity="center"
  11. android:stretchMode="columnWidth" >
  12. </GridView>
Step 2
Create an another XML file with this:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. <ImageView
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:id="@+id/imageView"
  10. />
  11. </LinearLayout>
Step 3
Create a Java class file MainActivity.java with this:
  1. package com.gridview;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.AdapterView;
  7. import android.widget.GridView;
  8. public class MainActivity extends Activity {
  9. @Override
  10. public void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_main);
  13. GridView gridView = (GridView) findViewById(R.id.gridview1);
  14. Adapter adapter=new Adapter(this);
  15. gridView.setAdapter(adapter);
  16. gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  17. @Override
  18. public void onItemClick(AdapterView<?> adapterView, View view, int index, long l) {
  19. Intent i=new Intent(MainActivity.this,Imageset.class);
  20. i.putExtra("1",index);
  21. startActivity(i);
  22. }
  23. });
  24. }
  25. }
Step 4
Create another Java Class file:
  1. package com.gridview;
  2. import android.content.Context;
  3. import android.view.View;
  4. import android.view.ViewGroup;
  5. import android.widget.BaseAdapter;
  6. import android.widget.GridView;
  7. import android.widget.ImageView;
  8. public class Adapter extends BaseAdapter {
  9. private Context mycontext;
  10. Integer[] pictures={
  11. R.drawable.pic1,
  12. R.drawable.pic2,
  13. R.drawable.pic3,
  14. R.drawable.pic4,
  15. R.drawable.pic5 ,
  16. R.drawable.pic6,
  17. R.drawable.pic7,
  18. R.drawable.pic8,
  19. R.drawable.pic9,
  20. } ;
  21. public Adapter(Context p)
  22. {
  23. mycontext=p;
  24. }
  25. @Override
  26. public int getCount() {
  27. return pictures.length;
  28. }
  29. @Override
  30. public Object getItem(int index) {
  31. return pictures[index];
  32. }
  33. @Override
  34. public long getItemId(int i) {
  35. return 0;
  36. }
  37. @Override
  38. public View getView(int index, View view, ViewGroup viewGroup) {
  39. ImageView imageView = new ImageView(mycontext);
  40. imageView.setImageResource(pictures[index]);
  41. imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
  42. imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
  43. return imageView;
  44. }
  45. }
Step 5
Create another Java class and write this:
  1. package com.gridview;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.widget.ImageView;
  6. public class Imageset extends Activity {
  7. // ImageView imageview;
  8. public void onCreate(Bundle savedInstanceState )
  9. {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.imageset);
  12. // imageview=(ImageView)findViewById(R.id.imageView);
  13. Intent i = getIntent();
  14. // Selected image id
  15. int position = i.getExtras().getInt("1");
  16. Adapter imageAdapter = new Adapter(this);
  17. ImageView imageView = (ImageView) findViewById(R.id.imageView);
  18. imageView.setImageResource(imageAdapter.pictures[position]);
  19. }
  20. }
Step 6
Output
1.jpg
Step 7
When you will click on the image:
2.jpg