GridView in Android Studio

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.    
  13. </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.    
  9. public class MainActivity extends Activity {  
  10.    
  11.     @Override  
  12.     public void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.activity_main);  
  15.    
  16.         GridView gridView = (GridView) findViewById(R.id.gridview1);  
  17.    
  18.         Adapter adapter=new Adapter(this);  
  19.         gridView.setAdapter(adapter);  
  20.       gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {  
  21.           @Override  
  22.           public void onItemClick(AdapterView<?> adapterView, View view, int index, long l) {  
  23.    
  24.               Intent i=new Intent(MainActivity.this,Imageset.class);  
  25.               i.putExtra("1",index);  
  26.               startActivity(i);  
  27.    
  28.           }  
  29.       });  
  30.     }  
Step 4
 
Create another Java Class file:
  1. package com.gridview;  
  2.    
  3. import android.content.Context;  
  4. import android.view.View;  
  5. import android.view.ViewGroup;  
  6. import android.widget.BaseAdapter;  
  7. import android.widget.GridView;  
  8. import android.widget.ImageView;  
  9.    
  10.    
  11. public class Adapter extends BaseAdapter {  
  12.    
  13.     private Context mycontext;  
  14.    
  15.     Integer[] pictures={  
  16.             R.drawable.pic1,  
  17.             R.drawable.pic2,  
  18.             R.drawable.pic3,  
  19.             R.drawable.pic4,  
  20.             R.drawable.pic5 ,  
  21.             R.drawable.pic6,  
  22.             R.drawable.pic7,  
  23.             R.drawable.pic8,  
  24.             R.drawable.pic9,  
  25. } ;  
  26.    
  27.     public Adapter(Context p)  
  28.     {  
  29.         mycontext=p;  
  30.     }  
  31.    
  32.     @Override  
  33.     public int getCount() {  
  34.         return pictures.length;  
  35.     }  
  36.    
  37.     @Override  
  38.     public Object getItem(int index) {  
  39.         return pictures[index];  
  40.     }  
  41.    
  42.     @Override  
  43.     public long getItemId(int i) {  
  44.         return 0;  
  45.     }  
  46.    
  47.     @Override  
  48.     public View getView(int index, View view, ViewGroup viewGroup) {  
  49.         ImageView imageView = new ImageView(mycontext);  
  50.    
  51.         imageView.setImageResource(pictures[index]);  
  52.         imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);  
  53.         imageView.setLayoutParams(new GridView.LayoutParams(7070));  
  54.         return imageView;  
  55.     }  
Step 5
 
Create another Java class and write this:
  1. package com.gridview;  
  2.    
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.widget.ImageView;  
  7.    
  8.    
  9. public class Imageset extends Activity {  
  10.    
  11.    // ImageView imageview;  
  12.    
  13.     public void onCreate(Bundle savedInstanceState )  
  14.     {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.imageset);  
  17.       //  imageview=(ImageView)findViewById(R.id.imageView);  
  18.    
  19.         Intent i = getIntent();  
  20.    
  21.         // Selected image id  
  22.          int position = i.getExtras().getInt("1");  
  23.          Adapter imageAdapter = new Adapter(this);  
  24.    
  25.          ImageView imageView = (ImageView) findViewById(R.id.imageView);  
  26.          imageView.setImageResource(imageAdapter.pictures[position]);  
  27.     }  
  28.    
Step 6
 
Output
 
1.jpg
 
Step 7
 
When you will click on the image:
 
2.jpg