Kotlin GridView example: Show List of Items on Grid | Android

GridView is an Android ViewGroup that can show items in a 2-dimensional, scrollable grid. The grid items will be inserted to the layout using an Adapter. In this tutorial, we’re gonna create an Android App that can show List of Items on a Grid using GridView.

More Practice: Kotlin GridView example: Show Item Details | Android

Related Post: Kotlin ListView example | Android

I. Technologies

– Android Studio 3
– Kotlin 1.1.51

II. Overview

1. Goal

We will build an example that uses GridView to show list of food items like this:

2. Project Structure


III. Practice

1. Set up Project

– Create New Project:

– Add each item insides sample_images to res/drawable folder.

2. Layout
2.1 Container Layout

Open res/layout/activity_main.xml file:

2.2 Item Layout

Add food_entry.xml file to res/layout folder:

3. Logic
3.1 Item Class

This class represents data of each Food Item:

3.2 Activity Class

We will:
– Create a BaseAdapter subclass.
– Set an instance of this class as data provider to the GridView.
– Return each cell’s view from getView() on your adapter.

IV. Source Code


By grokonez | December 2, 2017.

Last updated on July 13, 2018.

Related Posts

7 thoughts on “Kotlin GridView example: Show List of Items on Grid | Android”

  1. i have followed your instruction, but still get an error in “adapter = FoodAdapter(this, foodsList)”
    it says “Type missmatch : inferred type as fragmentHome but Context was expected”
    can you help me?

    thanks before

  2. Thank you very much ! I’ve never used a gridview before but after reading this I understand it all thank to you!

  3. Thank you, great sample. I have one single problem: in the method getView(…) the “position” parameter is not incremented. Do someoneelse have the same problem? I would appreciate any help. Greetings

Got Something To Say:

Your email address will not be published. Required fields are marked *