Android Tutorial – Gridview with Icon and Text

In this tutorial we are going to see how to create an advanced Gridview screen, that will have text + image

Lets Start:

The first thing we need to do, is create the layout we want to use for each of the items inside the GridView, in our case we want to have an Image and also a little text or title under the image. For this we will need to have inside a linear layout an ImageView and also a TextView as seen below:

grid_item.xml file

<linearlayout xmlns:android=""

   <imageview android:id="@+id/grid_item_image"

   <textview android:id="@+id/grid_item_text"


After having the grid item, we need the actual grid to put the item inside:

main.xml file

< ?xml version="1.0" encoding="utf-8"?>
<gridview xmlns:android=""


After all this is ready, we only need some code to glue it together

package firstdroid.tutorial.gridview;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class MyGridView extends Activity
   GridView MyGrid;
   public void onCreate(Bundle savedInstanceState){

      /*Here we setContentView() to main.xml, get the GridView and then fill it with the
                   ImageAdapter class that extend from BaseAdapter */

      MyGrid = (GridView)findViewById(;
      MyGrid.setAdapter(new ImageAdapter(this));
   public class ImageAdapter extends BaseAdapter
      Context MyContext;
      public ImageAdapter(Context _MyContext)
         MyContext = _MyContext;
      public int getCount()
                        /* Set the number of element we want on the grid */
         return 9;

      public View getView(int position, View convertView, ViewGroup parent)
         View MyView = convertView;
         if ( convertView == null )
                                /*we define the view that will display on the grid*/
            //Inflate the layout
            LayoutInflater li = getLayoutInflater();
            MyView = li.inflate(R.layout.grid_item, null);
            // Add The Text!!!
            TextView tv = (TextView)MyView.findViewById(;
            tv.setText("Item "+ position );
            // Add The Image!!!           
            ImageView iv = (ImageView)MyView.findViewById(;
         return MyView;

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

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

As we can see, to add a advanced grid view is very simple.

Any Question, Remark or Idea you want to add, please feel free to comment on this tutorial.

Attached a Zip with the code file

Using GridView Tutorial by Firstdroid