Search code examples
androidandroid-relativelayoutandroid-tablelayout

Android Relativelayout Programmatically


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!


Solution

  •  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