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.
Choose Mini Support SDK to use this project.
Lastly, Select Add No Activity in the last tab and click on Finish button.
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>
Want To Create An Android Application?
Validate your app idea and get a free quote.
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.
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.
You may also like,
This page was last edited on December 28th, 2020, at 8:39.