1

Basically I'm attempting to add rows to a table, I need to do this programmatically.

I can make it add the content I want, but not exactly how I want it.

For example I want a textview on the left with a button edittext button on the right.

Basically I want it like the image below:

.

Also I don't want to just and width to the textview.

Anyway here's what I've made so far:

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:padding="5dp" xmlns:android="http://schemas.android.com/apk/res/android">

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <Button
                android:id="@+id/btnOutstandingJob"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Outstanding Job" />

            <Button
                android:id="@+id/btnVanStock"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Van Stock" />

            <Button
                android:id="@+id/btnRegister"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:text="Register User" />
        </LinearLayout>

    </ScrollView>

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <Button
            android:id="@+id/btnLogout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginTop="5dp"
            android:text="Log Out" />
    </RelativeLayout>

</LinearLayout>

Java:

TableLayout table = (TableLayout) findViewById(R.id.tableLayout1);

TableRow row = new TableRow(vanstock.this);

RelativeLayout RowLayout = new RelativeLayout(vanstock.this);
RowLayout.setId(99);

TextView lblItem = new TextView(vanstock.this);
lblItem.setId(1);
lblItem.setText("ddfsgsdfgs");

RelativeLayout.LayoutParams lblitemParam = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
lblitemParam.addRule(RelativeLayout.CENTER_VERTICAL,
        RelativeLayout.TRUE);
lblitemParam.addRule(RelativeLayout.ALIGN_PARENT_LEFT,
        RelativeLayout.TRUE);

lblItem.setLayoutParams(lblitemParam);
RowLayout.addView(lblItem);

Button btnPlus = new Button(vanstock.this);
btnPlus.setId(4);
btnPlus.setText("+");
btnPlus.setWidth(40);

RelativeLayout.LayoutParams btnPlusParams = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
btnPlusParams.addRule(RelativeLayout.ALIGN_PARENT_TOP,
        RelativeLayout.TRUE);
btnPlusParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,
        RelativeLayout.TRUE);
btnPlus.setLayoutParams(btnPlusParams);

RowLayout.addView(btnPlus);

EditText txtItem = new EditText(vanstock.this);
txtItem.setId(3);
txtItem.setWidth(40);

RelativeLayout.LayoutParams txtItemParams = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
txtItemParams.addRule(RelativeLayout.ALIGN_PARENT_TOP,
        RelativeLayout.TRUE);
txtItemParams.addRule(RelativeLayout.LEFT_OF, btnPlus.getId());
txtItem.setLayoutParams(txtItemParams);

RowLayout.addView(txtItem);

Button btnMinus = new Button(vanstock.this);
btnMinus.setId(2);
btnMinus.setText("-");
btnMinus.setWidth(40);

RelativeLayout.LayoutParams btnMinusParams = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
btnMinusParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
btnMinusParams.addRule(RelativeLayout.LEFT_OF, txtItem.getId());
btnPlus.setLayoutParams(btnMinusParams);

RowLayout.addView(btnPlus);
row.addView(RowLayout);

table.addView(row, new LayoutParams(LayoutParams.WRAP_CONTENT,
        LayoutParams.WRAP_CONTENT));

I've tried searching etc., but I still can't get it to work correctly.. Any help would be appreciated!

1
  • I think you're making it too complicated. If I would make a structure like in the linked image, I would use a ListView with rows inflated from an xml file. This way all GUI stuff would be defined in the xml and you would only few details to do in Java. Commented Feb 6, 2012 at 14:16

1 Answer 1

2
 LayoutInflater inflater = getLayoutInflater();
 TableRow row = (TableRow) inflater.inflate(R.layout.table,
                _tablelayout, false);
 TextView textview = (TextView) row.findViewById(R.id.rowdata);

Like this way you can fullfill your requirement.Is this sufficient??

Best V.k

Sign up to request clarification or add additional context in comments.

2 Comments

I think I understand yes, so I make a xml with just a table row in? and then something like this? TableLayout table = (TableLayout) findViewById(R.id.tableLayout1); LayoutInflater inflater = getLayoutInflater(); TableRow row = (TableRow) inflater.inflate(R.layout.tablerow, table, false);
A little playing around after a few strange results.. however after playing around and I thought i put it back to how it was and now it's working perfect? :s confusing, but thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.