How to create a GridView in Android?

In this article, we are going to learn how to initiate a grid view for layout in Android?
Submitted by Manu Jemini, on December 12, 2017

GridView is a very easy way to show a similar type of data in grid formate. GridView provides very simple techniques to set it. For this tutorial to work, just create a new project with any name. You will get a default activity namely ‘MainActivity’, you can make use of it.

Starting with your XML file, drag and drop it from the widget section. There is no implication about the parent layout type. Make your layout the way you want it.

The second thing is your JAVA file. To begin with, you should import GridView from the widget, this is done by adding this line import android.widget.GridView; After that, in your activity, there would a default onCreate() function, this would be pre-set to use quickly.

Now, you would need to have a variable of type GridView and set it with the reference of the GridView in the layout file.

  • GridView gridView;
  • gridView=(GridView)findViewById(R.id.gridView);

Above Lines would do it. After doing everything up, your code should look like this. Connect an Android device or run an ADV and then build your project to see the result.

Java file:

package com.example.hp.demo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.GridView;


public class MainActivity extends AppCompatActivity
{
    GridView gridView;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        gridView=(GridView)findViewById(R.id.gridView);

    }
}

XML file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">


    <GridView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/gridView"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:numColumns="auto_fit" />
</RelativeLayout>

Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hp.demo">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Output

Android - GridView

Related Tutorials

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT


Top MCQs

Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.