---
title: "Using GridView in Android Application"  
description: "GridView is a ViewGroup that displays items in a two dimensional, scrollable grid. The grid items are automatically inserted to the layout using a Lis"  
author: "Chris Anderson"  
published: 2011-10-17  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/783/using-gridview-in-android-application  
category: "android"  
tags: ["android"]  
reading_time: 4 minutes  

---

# Using GridView in Android Application

[GridView](https://www.mindstick.com/articles/873/update-delete-edit-gridview-in-asp-dot-net) is a [ViewGroup](https://www.mindstick.com/interview/2633/what-is-viewgroup) that displays items in a two dimensional, scrollable grid. The grid items are automatically inserted to the layout using a **ListAdapter**.\

In this [article](https://yourviews.mindstick.com/view/81489/kashmir-now-after-an-year-of-abrogation-of-article-370) I am going to [explain](https://www.mindstick.com/forum/159699/what-is-a-compilation-error-in-dot-net-core-explain-with-an-example) how to create GridView in an [Android application](https://www.mindstick.com/articles/1653/creating-libraries-for-android-applications). In GridView [multiple](https://www.mindstick.com/blog/12797/iowa-is-expected-to-see-heavy-growth-in-multiple-sectors) images will display and when an image is selected, a toast [message](https://www.mindstick.com/interview/682/which-are-the-various-message-map-macros) will display the position of the image.

**Create a [project](https://yourviews.mindstick.com/story/1788/things-to-ensure-your-construction-project-is-successful)**

- Start a new project named GridViewDemo.
- Save the image files (image0.jpg, image1.jpg, image2.jpg, image3.jpg, image4.jpg, image5.jpg, image6.jpg, image7.jpg) into the project's res/drawable/ directory.
- Open the main.xml file inside the res/layout/. Insert the following:

```
<?xml  version="1.0" encoding="utf-8"?>
<GridView  xmlns:android="http://schemas.android.com/apk/res/android"
             android:id="@+id/gridview"
            android:layout_width="fill_parent"
             android:layout_height="fill_parent"
             android:columnWidth="90dp"
             android:numColumns="auto_fit"
             android:verticalSpacing="10dp"
             android:horizontalSpacing="10dp"
             android:stretchMode="columnWidth"
           android:gravity="center"/>
```

· Create a new class called ImageAdapter that extends BaseAdapter:

```
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class ImageAdapter  extends BaseAdapter
{
        private Context  mContext;
        private Integer[]  mThumbIds = {R.drawable.image0,R.drawable.image1,
              R.drawable.
             R.drawable.image2,R.drawable.image3,R.drawable.image4,R.drawable.image5,
             R.drawable.image6,R.drawable.image7};
        public ImageAdapter(Context c)
       {
               mContext=c;
       }
        public int getCount()
       {
               return mThumbIds.length;
       }
        public Object getItem(int position)
       {
              return null;
       }
        public long getItemId(int position)
       {
               return 0;
       }
        public View getView(int position, View convertView, ViewGroup parent)
       {
              ImageView imageView;
               if(convertView==null)
              {
                     imageView=new ImageView(mContext);
                     imageView.setLayoutParams(new GridView.LayoutParams(85,85));
                     imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                     imageView.setPadding(8, 8, 8, 8);
              }
               else
              {
                     imageView=(ImageView)convertView;
              }
              imageView.setImageResource(mThumbIds[position]);
               return imageView;
       }
}
```

· Open GridViewDemo.java and insert the following code:

```
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;

public class GridViewDemo  extends Activity {

     @Override
     public void onCreate(Bundle savedInstanceState)
    {
         super.onCreate(savedInstanceState);
        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)
        {
               Toast.makeText(GridViewDemo.this,  "" + (position+1),
                                                          Toast.LENGTH_SHORT).show();
        }
        });
    }
}
```

· Run the application.

Your [grid layout](https://www.mindstick.com/forum/156562/css-grid-layout-module) should look something like this:

![Using GridView in Android Application](https://www.mindstick.com/mindstickarticle/4ed9bee8-c214-471f-bcc5-3dd743dbb2ab/images/7aee086a-f1e9-4566-816e-3d451d266b69.png)

When you select an image the toast message display the position of a selected image.

\

---

Original Source: https://www.mindstick.com/articles/783/using-gridview-in-android-application

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
