Android TableLayout

TableLayout is a layout manager that arranges its child elements in rows and columns. A cell can be empty or has one element. The <TableRow> tag is used to define a row of the TableLayout. You can have many rows and columns as you want. Cells can span columns. Cells are added to a row in increasing column order. You can specify a column number for a child cell by using the layout_column attribute. For example, to place a child cell in the second column, you will write: android:layout_column="1". If you don't specify a column number for a child cell, it will auto-increment to the next available column. A column can be stretched (expanded in width to fit any extra space) by using the android:stretchColumns. For example, to stretch the first and second column of the table layout, you will write: android:stretchColumns="0,1". The indices of the columns start from 0. If you want to stretch all columns, simply place * in the double-quotes. A child element can be spanned to multiple columns by using android:layout_span. For example, to span a child element to occupy two columns, you will write: android:layout_span="2". You should note that in a child element, specifying the layout_width of element does not work. However, you can set the layout_height of the element.



In the example below, The TableLayout has three rows and three columns. The first row has two TextViews. The second row has two TextViews. The first TextView of the second row is placed in the second cell (1 index) so one cell is empty. The third row has a Button that spans two columns.

<TableLayout 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:stretchColumns="*"
  
   >
  
   <TableRow>
     <TextView
       android:text="Row1, Cell1"
     android:padding="5dip" />
     <TextView
       android:text="Row1, Cell2"
       android:padding="5dip"
         />    
    
</TableRow>
  
   <TableRow>
     <TextView
         android:layout_column="1"
         android:text="Row2, Cell1"
     android:padding="5dip"    
     />
     <TextView
        android:layout_column="2"
        android:text="Row2, Cell2"
       android:padding="5dip" />    
    
</TableRow>

   <TableRow>
     <Button
         android:text="Button"
     android:padding="5dip"    
       android:layout_span="2"  
     />
  
</TableRow>
  
  
</TableLayout>


Android-TableLayout

Posted by: Dara | post date: 08-17-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.