Anda di halaman 1dari 6

Android TableLayout example

http://www.mkyong.com/android/android-tablelayout-ex...

Java Forum

Advertise

Contact Us

RSS

Java Core

Web Frameworks

Spring

Hibernate

Web Service

Others...

Search

Android TableLayout example


Published: December 20, 2011 , Updated: January 12, 2012 , Author: mkyong

In Android, TableLayout let you arranges components in rows and columns, just like the standard table layout in HTML, <tr> and <td>. In this tutorial, we show you how to use TableLayout to arrange button, textview and edittext in rows and columns format, and also demonstrates the use of android:layout_span to span view in 2 cells, and android:layout_column to display the view in specified column.

Note

In Eclipse 3.7, XML code assist will not prompts the attribute android:layout_span, android:layout_column and many other useful TableLayout attributes, no idea why, may be bug. Just put the attribute inside, its still compile and run.

Live Demo Try your AJAX code online! Demo -> ZK Explorer Zkoss.org Analyze C# code coverage View code coverage in Visual Studio Coverage highlighting and reports jetbrains.com/dotcover Date Chinese Girls Sincere Chinese Girl Seek True Love Join to Find Her! www.ChnLove.asia

Mkyong en Facebook
Me gusta A 2,328 personas les gusta Mkyong.

P.S This project is developed in Eclipse 3.7, and tested with Android 2.3.3.

1. TableLayout
Open res/layout/main.xml file, add following views for demonstration. Read the XML comments, it should be self-explanatory.

Hassan

Mohammad

Ishtiyaq

Sameh

Wee

Sukhmeet

Anand

Abhishek

Dinesh

Amila

Plug-in social de Facebook

File : res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tableLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" > <!-- 2 columns --> <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dip" > <TextView android:id="@+id/textView1" android:text="Column 1" android:textAppearance="?android:attr/textAppearanceLarge" /> <Button android:id="@+id/button1" android:text="Column 2" /> </TableRow> <!-- edittext span 2 column --> <TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dip" >

1 de 6

27/02/12 17:05

Android TableLayout example

http://www.mkyong.com/android/android-tablelayout-ex...

<EditText android:id="@+id/editText1" android:layout_span="2" android:text="Column 1 &amp; 2" /> </TableRow> <!-- just draw a red line --> <View android:layout_height="2dip" android:background="#FF0000" /> <!-- 3 columns --> <TableRow android:id="@+id/tableRow3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dip" > <TextView android:id="@+id/textView2" android:text="Column 1" android:textAppearance="?android:attr/textAppearanceLarge" /> <Button android:id="@+id/button2" android:text="Column 2" /> <Button android:id="@+id/button3" android:text="Column 3" /> </TableRow> <!-- display this button in 3rd column via layout_column(zero based) --> <TableRow android:id="@+id/tableRow4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dip" > <Button android:id="@+id/button4" android:layout_column="2" android:text="Column 3" /> </TableRow> <!-- display this button in 2nd column via layout_column(zero based) --> <TableRow android:id="@+id/tableRow5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dip" > <Button android:id="@+id/button5" android:layout_column="1" android:text="Column 2" /> </TableRow> </TableLayout>

Subscribe Mkyong via email :

Subscribe

Latest Posts
Android WebView example Android TabLayout example How to import class automatically in Eclipse How to remove unused imports in Eclipse

2. Demo
See result.

Android GridView example How to display line numbers in Eclipse How to check if Date is within a certain range in Java? How to check if date is valid in Java Struts 2 dynamic image example try-with-resources example in JDK 7

Advertisement

As a market leader in the reseller hosting industry, micfo.com offers you a great opportunity to start your own web hosting company. Join them today.

Latest Comments
Navin on JSON example with Jersey + Jackson

2 de 6

27/02/12 17:05

Android TableLayout example

http://www.mkyong.com/android/android-tablelayout-ex...

How to write a service consuming a JSON string as a parameter. I mean, method should consume directl... Meethas on Maven + (Spring + Hibernate) Annotation + MySql Example Hi , I need a simple hibernate project without maven or ant and i need to run it in apache tomcat se... HibLearner on Hibernate fetching strategies examples Suppose I have an Order table with 50 columns and I have an Order class with 50 properties and mappe... mkyong on Connect to PostgreSQL with JDBC driver Make sure postgresql-9.1-901.jdbc3.jar is configured correctly and able to locate in your class path... mkyong on Struts 2 Hello World Example Not really related with above article, please post your question on JavaNullPointer.com? And elabora...

Download Source Code

Download it Android-TableLayout-Example.zip (15 KB)

References
1. 2. 3. 4. Android TableLayout example Android TableLayout JavaDoc Another great Android TableLayout example Set layout span through code

To Whom It May Concern, If you have any Java questions or problems, please post at this new JavaNullPointer.com forum. Best Regards, mkyong

Related Posts
Android TabLayout example

Popular Posts
Top 5 free Java eBooks

3 de 6

27/02/12 17:05

Android TableLayout example


Android GridView example Android WebView example Android ListView example How to set default activity for Android application

http://www.mkyong.com/android/android-tablelayout-ex...
Top 8 Java people you should know Top 20 Java websites you must visit Top 10 Java regular expression examples Top 10 open source forums in collection Top 5 open source Q&A systems

Java / J2EE Tutorials

3 Comments
manohar says:
January 7, 2012 at 6:33 pm

one more thing when ever design a screen first use scrollview layout and then add your layouts to it.it makes your screen scrollable even you turn your mobile into landscape
Reply

mkyong says:
January 16, 2012 at 10:19 pm

Nice tips. Thanks ~


Reply

manohar says:
January 7, 2012 at 6:31 pm

instead of using table layout go through the relative layout otherwise absolute layout
Reply

Leave a Reply
Name (required)

Mail (will not be published) (required)

Website

[Note] - To post source code in comment, wrap your source code like this :

1. Java - <pre lang="java"> Java codes here </pre> 2. XML - <pre lang="xml"> XML here </pre> 3. HTML - <pre lang="html4strict"> HTML here </pre>

4 de 6

27/02/12 17:05

Android TableLayout example

http://www.mkyong.com/android/android-tablelayout-ex...

Submit Comment
Notify me of followup comments via e-mail

Poll : What is your current Java web application framework?


Apache Struts Apache Struts2 Apache Tapestry Apache Wicket Custom in-house Framework Google Web Toolkit (GWT) JavaServer Faces (JSF 1.x) JavaServer Faces (JSF 2.x) JBoss Seam No framework, code in JSP Oracle Application Framework Others Spring MVC Spring WebFlow Stripes View Results

Vote

All Available Tutorials


Java Core Technologies : Java I/O, Java RegEx, Java XML, Java JSON, JDBC, Java Misc J2EE Frameworks : Hibernate, JSF 2.0, Spring Core, Spring MVC, Spring Security, Apache Wicket, Struts 1.x, Struts 2.x Web Service : JAX-WS (SOAP), JAX-RS (REST) Build Tools : Maven, Archiva Unit Test Frameworks : jUnit, TestNG Others... : jQuery, Java MongoDB

Favorites Links
DZone - Fresh Links Official Java EE 5 Tutorial Spring 2.5.x documentation Hibernate core documentation Java SE 6.0 API documentation Java EE 6.0 API documentation Java Secure Socket Extension (JSSE) Reference Guide JSP home page JSF home page Eclipse IDE for Java developer Maven home page Ant home page Struts 1.3 documentation Struts 2.2 documentation Maven central repository Java.Net Maven repository Martin Fowler

Friends & Links


Java Training JavaScript Training Java Web Hosting Java & Co. Tutorials PHP Tutorials TenthOfMarch Find Free Icons Web Security Blog

About Mkyong.com
Mkyong.com is about a person who is facing the big tree (Java web development), always wonder why the tree (Java) is so big!

All tutorials and examples are unique, and from my personal experience, if you find mistake in my tutorial, please correct me :) after all, we learned through the process. For Java question that is not related to the tutorial, you can post here - Java Q&A forum. 1. Twitter - Follow Me 2. Facebook - Fan Page 3. RSS - Subscribe It Advertise With Us

5 de 6

27/02/12 17:05

Android TableLayout example

http://www.mkyong.com/android/android-tablelayout-ex...

Copyright 2008-2011 Mkyong Enterprise, all rights reserved. Privacy Policy

6 de 6

27/02/12 17:05

Anda mungkin juga menyukai