Android GridLayout




Android GridLayout

1. Static GridLayout Creation in Android
2. Dynamic GridLayout Creation in Android

1. Static GridLayout Creation in Android
Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project Click Here.

Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code :

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:rowCount="3"
    android:columnCount="3">


    <TextView
        android:id="@+id/text1"
        android:layout_row="0"
        android:layout_column="0"
        android:text="[0,0]"
        android:textSize="25dp"
        android:padding="25dp"
        />

    <TextView
        android:id="@+id/text2"
        android:layout_row="0"
        android:layout_column="1"
        android:text="[0,1]"
        android:textSize="25dp"
        android:padding="25dp"
        />

    <TextView
        android:id="@+id/text3"
        android:layout_row="0"
        android:layout_column="2"
        android:text="[0,2]"
        android:textSize="25dp"
        android:padding="25dp"
        />

    <TextView
        android:id="@+id/text4"
        android:layout_row="1"
        android:layout_column="0"
        android:text="[1,0]"
        android:textSize="25dp"
        android:padding="25dp"
        />

    <TextView
        android:id="@+id/text5"
        android:layout_row="1"
        android:layout_column="1"
        android:text="[1,1]"
        android:textSize="25dp"
        android:padding="25dp"
        />

    <TextView
        android:id="@+id/text6"
        android:layout_row="1"
        android:layout_column="2"
        android:text="[1,2]"
        android:textSize="25dp"
        android:padding="25dp"
        />

    <TextView
        android:id="@+id/text7"
        android:layout_row="2"
        android:layout_column="0"
        android:text="[2,0]"
        android:textSize="25dp"
        android:padding="25dp"
        />

    <TextView
        android:id="@+id/text8"
        android:layout_row="2"
        android:layout_column="1"
        android:text="[2,1]"
        android:textSize="25dp"
        android:padding="25dp"
        />

    <TextView
        android:id="@+id/text9"
        android:layout_row="2"
        android:layout_column="2"
        android:text="[2,2]"
        android:textSize="25dp"
        android:padding="25dp"
        />

</GridLayout>

Step 3 : Open src -> package -> MainActivity.java and add following code :

package com.gudivada.hemanthsomaraju;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

TextView t1, t2, t3, t4, t5, t6, t7, t8, t9;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
   
        t1 = (TextView) findViewById(R.id.text1);
        t2 = (TextView) findViewById(R.id.text2);
        t3 = (TextView) findViewById(R.id.text3);
        t4 = (TextView) findViewById(R.id.text4);
        t5 = (TextView) findViewById(R.id.text5);
        t6 = (TextView) findViewById(R.id.text6);
        t7 = (TextView) findViewById(R.id.text7);
        t8 = (TextView) findViewById(R.id.text8);
        t9 = (TextView) findViewById(R.id.text9);
   
        t1.setClickable(true);
        t2.setClickable(true);
        t3.setClickable(true);
        t4.setClickable(true);
        t5.setClickable(true);
        t6.setClickable(true);
        t7.setClickable(true);
        t8.setClickable(true);
        t9.setClickable(true);
   
        t1.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub

Toast.makeText(getBaseContext(), "[0,0] Clicked",
Toast.LENGTH_SHORT).show();
}
});
   
        t2.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub

Toast.makeText(getBaseContext(), "[0,1] Clicked",
Toast.LENGTH_SHORT).show();
}
});
   
        t3.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub

Toast.makeText(getBaseContext(), "[0,2] Clicked",
Toast.LENGTH_SHORT).show();
}
});
   
        t4.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub

Toast.makeText(getBaseContext(), "[1,0] Clicked",
Toast.LENGTH_SHORT).show();
}
});
   
        t5.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub

Toast.makeText(getBaseContext(), "[1,1] Clicked",
Toast.LENGTH_SHORT).show();
}
});
   
        t6.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub

Toast.makeText(getBaseContext(), "[1,2] Clicked",
Toast.LENGTH_SHORT).show();
}
});
   
        t7.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub

Toast.makeText(getBaseContext(), "[2,0] Clicked",
Toast.LENGTH_SHORT).show();
}
});
   
        t8.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub

Toast.makeText(getBaseContext(), "[2,1] Clicked",
Toast.LENGTH_SHORT).show();
}
});
   
        t9.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub

Toast.makeText(getBaseContext(), "[2,2] Clicked",
Toast.LENGTH_SHORT).show();
}
});    
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}


Step 4 : Open AndroidManifest.xml and add following code :

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

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.gudivada.hemanthsomaraju.MainActivity"
            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>


Step 5 : Our output will be like this :

 








2. Dynamic GridLayout Creation in Android

Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project Click Here.

Step 2 : Open src -> package -> MainActivity.java and add following code :

package com.gudivada.hemanthsomaraju;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.GridLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

GridLayout gl;
TextView[] text;
int item;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
   
        gl = new GridLayout(MainActivity.this);
        gl.setLayoutParams(new LayoutParams
(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        gl.setOrientation(0);
        gl.setColumnCount(3);
        gl.setRowCount(3);
   
        text = new TextView[9];
   
        for(int i=0;i<9;i++)
        {
         text[i] = new TextView(MainActivity.this);
         text[i].setLayoutParams(new LayoutParams
(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
         text[i].setText(String.valueOf(i));
         text[i].setTextSize(25);
         text[i].setPadding(50, 25, 10, 25);
         gl.addView(text[i]);
        }
   
        setContentView(gl);
   
        for(item=0;item<9;item++)
        {
         text[item].setOnClickListener(new View.OnClickListener() {
        
int pos = item;

public void onClick(View v) {
// TODO Auto-generated method stub

Toast.makeText(getBaseContext(), pos+" Clicked",
Toast.LENGTH_SHORT).show();
}
});
        }    
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}


Step 3 : Open AndroidManifest.xml and add following code :

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

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.gudivada.hemanthsomaraju.MainActivity"
            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>

Step 4 : Our output will be like this :