Sunday, 23 March 2014

GridView Creation in Android Application

Simple GridView

This blog describe about display data in simple gridView.

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#7CFCff"
    android:orientation="vertical" >



    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Static Grid View"
        android:layout_gravity="center"
        android:textSize="20sp"
        android:layout_marginTop="20dp"
        android:textColor="#000000"
        android:textStyle="bold" />

    <GridView
        android:id="@+id/gridView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       android:layout_marginLeft="20dp"
       android:layout_marginTop="40dp"
       android:layout_marginRight="20dp"
      
        android:background="#000000"
        android:numColumns="3" >
    </GridView>

</LinearLayout>

GridViewActivity.java

package com.rakesh.tiwari.gridview;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.Toast;

public class GridViewActivity extends Activity {
List<String> list;
GridView grid;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        list=new ArrayList<String>();
        grid=(GridView)findViewById(R.id.gridView1);
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        list.add("5");
        list.add("6");
        list.add("7");
        list.add("8");
        list.add("9");
        list.add("10");
        list.add("11");
        list.add("12");
       
        ArrayAdapter<String> aradptr=new ArrayAdapter<String>(getApplicationContext(),
                android.R.layout.simple_dropdown_item_1line,list);
        grid.setAdapter(aradptr);
        grid.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), list.get(arg2), Toast.LENGTH_LONG).show();   
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.grid_view, menu);
        return true;
    }

}

AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.rakesh.tiwari.gridview"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:screenOrientation="portrait"
            android:name="com.rakesh.tiwari.gridview.GridViewActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>



GridView Android App
GridView Android App

GridView  App
GridView  App

Simple GridView
Simple GridView