Quantcast
Channel: AuthorCode » Android programming
Viewing all articles
Browse latest Browse all 7

Day 5 – Using table layout to display child View elements

$
0
0



In the last post we have seen how we can use relative layout to organize view elements. And this post demonstrate how to display view elements in the table layout.
It is same as creating table in html.
So lets start…

1. Start a new project.
2. write the following code in the main.xml that is located in res/layout/ folder.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="fill_parent"
  4.     android:layout_height="fill_parent"
  5.     android:stretchColumns="1">
  6.     <TableRow>
  7.         <TextView
  8.             android:layout_column="1"
  9.             android:text="New"
  10.             android:padding="3dip" />
  11.         <TextView
  12.             android:text="Ctrl-O"
  13.             android:gravity="right"
  14.             android:padding="3dip" />
  15.     </TableRow>
  16.  
  17.     <TableRow>
  18.         <TextView
  19.             android:layout_column="1"
  20.             android:text="Open from File.."
  21.             android:padding="3dip" />
  22.         <TextView
  23.             android:text="Ctrl-S"
  24.             android:gravity="right"
  25.             android:padding="3dip" />
  26.     </TableRow>
  27.  
  28.     <TableRow>
  29.         <TextView
  30.             android:layout_column="1"
  31.             android:text="Save As.."
  32.             android:padding="3dip" />
  33.         <TextView
  34.             android:text="Ctrl-Shift-S"
  35.             android:gravity="right"
  36.             android:padding="3dip" />
  37.     </TableRow>
  38.  
  39.      <View
  40.         android:layout_height="2dip"
  41.         android:background="#FF909090" />
  42.  
  43.     <TableRow>
  44.         <TextView
  45.             android:layout_column="1"
  46.             android:text="Quit"
  47.             android:padding="3dip" />
  48.     </TableRow>
  49. </TableLayout>



We are using TableLayout like as HTML <table>. and TableRow as <tr> and in the place of <td> we are using view element (TextView). We can use any kind of View element. In between some of the rows, there is also a basic View, which is used to draw a horizontal line.


And now load the main.xml in java file like as:

  1. package my.authorcode;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5.  
  6. public class MyProjectActivity extends Activity {
  7.     /** Called when the activity is first created. */
  8.     @Override
  9.     public void onCreate(Bundle savedInstanceState) {
  10.         super.onCreate(savedInstanceState);
  11.         setContentView(R.layout.main);
  12.     }
  13. }


And in the last run the application, output would be:

Thanks for reading, check out AuthorCode for more Authorcode contents


Viewing all articles
Browse latest Browse all 7

Latest Images

Trending Articles





Latest Images