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.
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:stretchColumns="1"> <TableRow> <TextView android:layout_column="1" android:text="New" android:padding="3dip" /> <TextView android:text="Ctrl-O" android:gravity="right" android:padding="3dip" /> </TableRow> <TableRow> <TextView android:layout_column="1" android:text="Open from File.." android:padding="3dip" /> <TextView android:text="Ctrl-S" android:gravity="right" android:padding="3dip" /> </TableRow> <TableRow> <TextView android:layout_column="1" android:text="Save As.." android:padding="3dip" /> <TextView android:text="Ctrl-Shift-S" android:gravity="right" android:padding="3dip" /> </TableRow> <View android:layout_height="2dip" android:background="#FF909090" /> <TableRow> <TextView android:layout_column="1" android:text="Quit" android:padding="3dip" /> </TableRow> </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:
package my.authorcode; import android.app.Activity; import android.os.Bundle; public class MyProjectActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } }
And in the last run the application, output would be: