Tablelayout in Linearlayout view in Android

Many android developer come across mixing views and sometimes creating dynamic controls at runtime. In my example, you will see how a table can be embeded in LinerLayout and create some controls at runtime.
Here is the XML
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android=""
    android:layout_height="wrap_content" android:layout_width="fill_parent">
    <LinearLayout xmlns:android=""
        android:layout_height="wrap_content" android:layout_width="fill_parent"
        android:isScrollContainer="true" android:orientation="vertical">
<TextView android:id="@+id/heading" android:text="Heading"
android:textSize="25dip" android:layout_width="wrap_content"
<TextView android:id="@+id/name" android:text="Name:"
android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<EditText android:id="@+id/editName"
android:text="" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:inputType="textNoSuggestions"></EditText>
<HorizontalScrollView xmlns:android=""
            android:layout_width="wrap_content" android:layout_height="fill_parent">
<TableLayout android:layout_width="fill_parent"
                android:id="@+id/itemDetails" android:layout_height="wrap_content">
<TextView android:id="@+id/details" android:text="Details"></TextView>
<LinearLayout android:orientation="horizontal"
            android:layout_width="match_parent" android:layout_height="wrap_content">
<Button android:id="@+id/cancel" android:text="Cancel"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<Button android:id="@+id/confirm" android:text="Confirm"
android:layout_width="wrap_content" android:layout_height="wrap_content" />

and here is the java code.

private void createItemDetailsControls() {
TableLayout layout = (TableLayout)findViewById(;
for (int f=0; fTableRow tr = new TableRow(this);
for (int c=0; c EditText b = new EditText (this);
tr.addView(b, 100, 50);
} // for
} // for
} // ()

Let me know if you find it useful.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at

Up ↑

%d bloggers like this: