Android GridView


A gridview is an adapterview that shows items in a two-dimensional grid.

Android GridView

These grids are scrollable. GridView is an adapterview because it works with adapters. Adapters can then bind data to the gridview.

This is important because then it means that the gridview is decoupled from it's data source. So you can customize the gridview as you like without interfering with the data source. You can also work on the data source without affecting the visual appearance of the gridview.

GridView derives from AbsListview.

public class GridView extends android.widget.AbsListView{}

Both reside in the android.widget packages.

AbsListview is an abstract class and you won't use in directly. You'll use it in case you want to implement your own custom view with unique properties.

Even the sister of GridView which is ListView also directly derives from AbsListview. So AbsListview is a base class. It's used to implement virtualized list of items. Given it's virtualized you can implement it to show lists in various forms:

  • Lists -vertical and horizontal
  • Grids
  • Carousel
  • Stack

And so on.

So in our case the gridview uses it to display items in a grid. And we call the implementation the Gridview.

Some of the typical uses of gridview include:

  • Showing image gallery.
  • Showing list of movies.
  • Showing list of products etc.

Like other views and widgets, gridview are first defined in the xml layout. This is because android views and widgets are normally defined using XML, a markup language. Though it's also possible to create the views and widgets using raw java. However, XML has several advanatges:

  1. It's easier compared to having to remember the various APIs for creating views programmatically.
  2. It's more flexible and customizable.
  3. It decouples the user interface from the application logic. This leads to maintenable code.
  4. It's more reusable. You can easily copy paste XML code from online and reuse as opposed to java code.
  5. We can use the designer to generate the XML code.

To work with XML, first we add the gridview xml definition inside our layout:

    <?xml version="1.0" encoding="utf-8"?>
    <GridView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/gridview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnWidth="90dp"
        android:numColumns="auto_fit"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:stretchMode="columnWidth"
        android:gravity="center"
    />

Then we can move to our java code and reference the gridview and set its adapter:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new MyAdapter(this));

        gridview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {
                Toast.makeText(HelloGridView.this, "" + position,
                        Toast.LENGTH_SHORT).show();
            }
        });
    }

Top Android GridView Examples

Let's look at some examples.

Android Custom GridView with CardViews,with images and text tutorial. Hi.Here's our plan:

  • Custom GridView With CardViews as our ViewItems.
  • Each cardview as our viewitem shall have images and text.
  • We then handle ItemClicks.
  • We are using BaseAdapter as our adapter of choice.

This is a simple GridView tutorial.Here is what we do:

  • Fill GridView with data from a simple Array.
  • We use ArrayAdapter.
  • Handle ItemClicks.

Android GridView CRUD tutorial.

Hello.We see how to perform basic CRUD operations against GridView.

  • We add,update and delete.
  • Our basic data source is an arraylist.
  • Our adapter is ArrayAdapter.

Lets see how to sort data in android java using Collections class.We shall sort ascending and descending in GridView when button is clicked.

Hello friends.Today we see how to work with a simple gridview and objects.In short here is what we do:

  1. Fill list of objects to an arraylist.
  2. Bind the list to gridview via arrayadapter.
  3. Handle ItemClick events.

Android Simple GridView pagination. How to apply next/previous type of pagination to a simple GridView.

Introduction

This tutorial explores how to apply a next/previous type of pagination to a simple GridView The gridview comprises autogenerated data. Please watch the video tutorial below.

Hello guys.This is an absolute beginner practical example of a simple gridview and arraylist and handling itemclicklisteners.

In this class we look at how to sort a gridview in both ascending and descending manner.

Android GridView Examples.

Android GridView Population from an Array Tutorial.

Tools Used

These are the tools we used to create the project:

  1. Language : Java.

  2. Platform : Android.

  3. IDE : Android Studio.

  4. OS : Windows 8.1

Let's go.

Best Regards, Oclemy.

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think


Previous Post Next Post