Creating Table Layout

The project describes how to implement Table layout. TableLayout is a ViewGroup that displays child View elements in rows and columns.

Underlying Algorithm:

Basic description of algorithm in step by step form:

1.) Create a Project TableLayoutExample
2.) Open and insert following in main.xml:

 

android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">

<TextView
android:layout_column="1"
android:text="Sound On"
android:padding="3dip" />

<View
android:layout_height="2dip"
android:background="#FF909090" />

<TextView
android:layout_column="1"
android:text="Vibration On"
android:padding="3dip" />

<View
android:layout_height="2dip"
android:background="#FF909090" />

<TextView
android:layout_column="1"
android:text="Reset Game"
android:padding="3dip" />

<View
android:layout_height="2dip"
android:background="#FF909090" />

<TextView
android:layout_column="1"
android:text="Languages"
android:padding="3dip" />

<View
android:layout_height="2dip"
android:background="#FF909090" />

<TextView
android:layout_column="1"
android:text="Back"
android:padding="3dip" />

 

 

3.) Open and insert following in strings.xml:

 

Options menu in table layout!
Options menu in table layout

 

 

4.) Run the application.

Steps to Create:

1.) Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name i.e. TableLayoutExample. Enter the following information:

Project name: TableLayoutExample
Build Target: Android 2.3.3
Application name: TableLayoutExample
Package name: org.example.TableLayoutExample
Create Activity: TableLayoutExample

On Clicking Finish TableLayoutExample code structure is generated with the necessary Android Packages being imported along with TableLayoutExample.java. TableLayoutExample class will look like following:

 

package org.example.TableLayoutExample;
import android.app.Activity;
import android.os.Bundle;

public class TableLayoutExample extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}

 

 

Output – The final output:

Leave a Comment:

3 comments
Beth says June 16, 2011

How would you add a horizontal scroll to this? Thanks.

Reply
Sushant says June 17, 2011

Add

[code lang="xml"]
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android&quot;
android:layout_width="100px" android:layout_height="fill_parent" android:visibility="visible"
android:focusable="true">
<TableLayout ........ >
-
-
</tablelayout>
</HorizontalScrollView>
[/code]

You will be able to scroll the table horizontally.

Reply
Svetlana says February 6, 2012

Hi, nice detailed tutorial! We`ve posted tutorial on Android on our blog that your readers might also like: http://www.learncomputer.com/tutorials/
Thank you.

Reply
Add Your Reply