GridView is used to display items in a two-dimensional, scrollable grid form. The grid items are automatically inserted to the layout using a Adapter.

For example, displaying grid of icons. Item is represented by a single item view. This pattern is quite handy as it shows several icons on screen at the same time. Its helps displaying grids of data in a very simple and scrollable grid manner.

The simplest way to display a grid view is using ArrayAdapter class . You can add lists or arrays of custom objects to it and these objects are referenced using index and displayed in the TextView of GridView.

We set the ArrayAdapter to the GridView to display the items of array adapter in it .

The following example explains the usage of GridView using ArrayAdapter.

Step1: Create the XML for the activity which contains a GridView

main.xml

Step 2:  Create the Activity JAVA and use the code mentioned below.

  • Create the ArrayAdapter and add a default layout grid style and string array of data to it.
  • Set GridView adapter as the ArrayAdapter.
  • You can set listeners on your grid view also.