Android development from getting started to giving up (10) adapting GridView

This blog discusses how to display a GridView. In the first two blogs, we used ListView. When displaying a ListView, we used the Adapter. The function of the Adapter is to adapt the data source to the list control. A ListAdapter is also required to display the GridView.

Let's start with activity_main.xml code

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


The above layout defines a GridView and sets the columnWidth to a fixed value. android:numColumns="auto_fit" means that the Android system determines how many columns the GridView should display.

The properties related to GridView will be listed at the bottom and introduced briefly. Next, let's look at the code. Here's mainactivity Java code

import android.os.Bundle;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private static final String[] myListItems={"To", "be",
            "or", "not", "to", "be","that", "is", "the", "question"};

    protected void onCreate(Bundle savedInstanceState) {

        GridView gridView = (GridView) findViewById(;
        ListAdapter adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,myListItems);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(MainActivity.this, myListItems[i], Toast.LENGTH_SHORT).show();

See, this is the same routine. Define an array or list (myListItems array above), specify a layout for displaying each item (android.R.layout.simple_list_item_1 above), and finally instantiate a ListAdapter object and set it as the Adapter of GridView to display an array through GridView.

Next, an instance of an anonymous inner class is assigned to the gridView through the setOnItemClickListener method. The onItemClick method in the anonymous inner class will be called when each element is clicked.

Here are some properties related to GridView

  • android:numColumns: specifies how many columns the GridView displays. It can be set to a specific number or auto_fit, which means that the Android system determines how many columns to display according to factors such as column width, interval between columns, screen size, etc.
  • android:columnWidth: Specifies the width of each column
  • Android: vertical spacing: Specifies the spacing between rows
  • android:horizontalSpacing: Specifies the spacing between columns
  • android:stretchMode: if android:columnWidth is set to auto_fit, and if there is extra space in the horizontal direction after allocating the columns, you can use this attribute to decide how to allocate the remaining space. There are two options to set. If it is set to columnWidth, the remaining space will be allocated to each column. If it is set to spacingWidth, the remaining space will be allocated to the spacing between columns.

Keywords: Android

Added by Morthian on Mon, 20 Dec 2021 21:18:23 +0200