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="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content" android:layout_width="fill_parent">
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/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"
android:layout_height="wrap_content"></TextView>
<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="http://schemas.android.com/apk/res/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">
<TableRow>
<TextView android:id="@+id/details" android:text="Details"></TextView>
</TableRow>
</TableLayout>
</HorizontalScrollView>
<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" />
</LinearLayout>
</LinearLayout>
</ScrollView>


and here is the java code.

private void createItemDetailsControls() {
TableLayout layout = (TableLayout)findViewById(R.id.itemDetails);
layout.setPadding(1,1,1,1);
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
layout.addView(tr);
} // for
} // ()

Let me know if you find it useful.

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 WordPress.com.

Up ↑

%d bloggers like this: