In Android , grid view that lets you display components in a two dimensional scrolling grid Gridview is mainly useful when we want show data in grid layout like displaying images or icons
In this example you will see the months names stored in string variable are displayed in gridview
Lets See an Example
file :- grid_layout.xml
In this example you will see the months names stored in string variable are displayed in gridview
Lets See an Example
1. XML Layout
<?xml version="1.0" encoding="utf-8"?> <GridView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/grid_id" android:layout_width="match_parent" android:layout_height="match_parent" android:numColumns="auto_fit" android:columnWidth="100dp" android:stretchMode="columnWidth" />
2. Activity
file:- Grid_activity.java
package com.tutorialsbuzz.simplegridview; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.GridView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { String[] data = new String[]{"jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; GridView mGridView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ArrayAdapter aa = new ArrayAdapter(this, android.R.layout.simple_list_item_1, data); mGridView = (GridView) findViewById(R.id.grid_id); mGridView.setAdapter(aa); mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { Toast.makeText(MainActivity.this, data[i] + " item click", Toast.LENGTH_SHORT).show(); } }); } }
3. Android Manifest
file : AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.tutorialsbuzz.simplegridview"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" 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>
No comments:
Post a Comment