Building Two-Dimensional Scrolling List Using Android GridView in Android App

If you’re familiar with Android’s listview layout component, then you should feel like home here as well. The Android GridView is quite similar to listview component. It basically helps to build two-dimensional, easy-to-browse scrolling lists. You might have noticed gridviews in many apps such as Instagram, Snapchat, PayTM, which means it’s a necessary skill. And after following this tutorial step-by-step, you’ll be able to implement this layout in your native Android app.

Let’s Get Started

Create a new project under file menu, modify the project details, and choose appropriate location for the demo.

ssgridviewdemo1

Choose Mini Support SDK to use this project.

ssgridviewdemo2

Lastly, Select Add No Activity in the last tab and click on Finish button.

ssgridviewdemo3

Create XML File

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridView"
android:numColumns="auto_fit"
android:gravity="center"
android:columnWidth="50dp"
android:stretchMode="columnWidth"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</GridView>

Create Class For MainActivity

package com.gridviewdemo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
GridView gridView;
static final String[] numbers = new String[]{
"1", "2", "3", "4", "5",
"6", "7", "8", "9", "10",
"11", "12", "13", "14", "15",
"16", "17", "18", "19", "20"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.gridView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, numbers);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(getApplicationContext(),
((TextView) v).getText(), Toast.LENGTH_SHORT).show();
}
});
}
}

Now when you run the demo, it should look like this.

 

ssgridviewdemo4

And Done!

So now that you know the process of implementing Android Gridview in an Android app, Go Ahead and start implementing it. And in case you’re building an Android app from scratch, and you need to consult with an expert person, hire Android app development company, discuss your app idea, and build your app with the right people.

Grab a free copy of Gridview Android Example Demo from Github.

This page was last edited on June 19th, 2018, at 3:19.
 
 

Want to Develop Android App From Scratch? Contact Us Now

Get your free consultation now