Android GridView Tutorials with Example

By | May 30, 2019

GridView in Android Tutorial Example

What is GridView ?

GridView is a display items in two-dimensional scrolling grid (rows & columns).GridView items are not necessarily predetermined but they are automatically inserted to the layout using a ListAdapter.

GridView is mainly useful when we want show data in grid layout like displaying images or icons. You can build application using GridView like Gallery. GridView and ListView are subclass of AdapterView.

It can be populated binding to an Adapter, It is retrieves data from an external source and creates a View that represents each data entry. GridView are used to BaseAdapter.

GridView Attribute

android:columnWidth : This attribute is used to fixed width for each can used to px, dp, sp, in, or mm.

android:gravity : This attribute is used to specify gravity within each cell to bottom,top,left,right,center,center_horizontal,center_vertical etc.

android:horizontalSpacing : This attribute is used to Define  the default horizontal spacing between columns. you can used to px, dp, sp, in, or mm.

android:numColumns : This attribute is used to Define how many number of columns display. It can integer value, such as (50) or auto_fit which means display as many columns as possible to fill the available space.

android:stretchMode : This attribute is used to columns should stretch to fill the available empty can define none, spacingWidth, columnWidth, spacingWidthUniform.

android:verticalSpacing : This attribute is used to vertical spacing between rows.

What is BaseAdapter in Android ? And How to Use BaseAdapter ?

BaseAdapter is a Bridge between UI and data.It is used to fill data in UI Component. .Common base class of common implementation for an Adapter that can be used in both ListView (by implementing the specialized ListAdapter interface) and Spinner (by implementing the specialized SpinnerAdapter interface).

BaseAdapter is a interconnection of UI and is pass data from UI and display data on UI screen is binding of data used to adapter.

GridView Example in Android- How to Implement GridView

Create activity_main.xml File following below the code.

Create File Following below the code.

Create File Following below the code.

Above the example of Declare gridView tag and then required some attribute in gridView tag.and then create MyAdapter class beacuse of i hava display images in i have used to adapter then this adapter extends of BaseAdapter and implement some methods to required.

Now i have defint integer array local variable and store images in my drawable in this images i have attach of Imageview and all images are position wise display to used getView() one is i have define image array size of counting and display image.and used to getCount() to adapter is set all images is a image array length.

finally i have declared gridview tag of file and set the adapter of gridview and then display images of gridview.