Android Grid View

In this tutorial you will learn about the Android Grid View and its application with practical example.

Android Grid View

It displays items in a two-dimensional , scrollable grid. The ListView and GridView are subclass of AdapterView . As we discussed in ListView that Adapter is a bridge between UI and Data source and it retrieves data from data source.

Table Of Contents

Lets understand it by an example , Create GridView in Xml Layout like below code

GridView Attributes

android:id : it used to identify GridView Uniquely.

android:numColumns : It used to define that how many columns will be shown on display. It may be integer value like “2” or auto_fit. auto_fit means as many as column possible to fit on display.

android:verticalSpacing : It defines vertical spacing between rows, It can be dp,px,sp,in or mm.

android:horizontalSpacing : It defines horizontal spacing between columns.

android:strechMode : It defines how columns should stretched to fill display.

Now lets see how your gridview will look in xml files.

gv

Now it’s time to do some coding part, You have to create Activity, Bean and an Adapter for display your GridView. Do not you worry about that all things are ready, you can try below code and run your code, you will get your gridview screen, just follow all steps.

 

Beans : JavaBeans are classes that encapsulate many objects into a single object. It contains constructor, Getter, Setter Methods. With the help of Setter and Getter method user can set and get data. It also have zero argument constructor. Check out below code:

Base Adapter: BaseAdapter which extends object and implements ListAdapter and Spinner Adapter. It can be used in ListView, GridView and Spinner. BaseAdapter is very generic adapter which allows you to do pretty much whatever you want . Its custom Adapter from which you can do whatever you want to do with grid and list, But you need to do some coding for it. Check below coding for Base Adapter.

Also design xml for Adapter view, Like I did check xml code:

At final Stage you have to do bind your GridView with Adapter. Lets finish this. Checkout below code :

Now the output of this code will look like below image.

finalgv

In this tutorial we have learn about the Android Grid View and its application with practical example. I hope you will like this tutorial.