0

I have a layout file called row which is basically a wrapper for how i want my row to look like

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TableRow
        android:id="@+id/tableRow"
        android:layout_height="wrap_content"
        android:layout_width="match_parent">

        <TextView
            android:id="@+id/oid"
            android:layout_width="0dp"
            android:layout_weight="1" android:background="#dcdcdc"
            android:textColor="#000000"
            android:padding="5dip"

            />
        <TextView
            android:id="@+id/value"
            android:layout_width="0dp"
            android:layout_weight="1" android:background="#d3d3d3"
            android:textColor="#000000"
            android:padding="5dip"

            />
        <TextView
            android:id="@+id/type"
            android:layout_width="0dp"
            android:layout_weight="1" android:background="#dcdcdc"
            android:textColor="#000000"
            android:padding="5dip"

            />
    </TableRow> 
</LinearLayout>

Now i d like to be able to create a row using this layout and place it in my table that is defined in the fragment layout. This is my current code for creating a row(it doesn't use the row layout file) and adding it to the table, but the row is not formatted as i d like it to be.

TableLayout varbindTable = (TableLayout) view2.findViewById(R.id.tableLayout1);
TableRow row = new TableRow(getActivity());

row.setLayoutParams(new TableRow.LayoutParams(
        TableRow.LayoutParams.MATCH_PARENT,
        TableRow.LayoutParams.MATCH_PARENT));
varbindTable.addView(row);
TextView name = new TextView(getActivity());

name.setText(((EditText)view.findViewById(R.id.oid)).getText().toString());
name.setTextColor(Color.BLACK);
name.setLayoutParams(new TableRow.LayoutParams(
        0,
        TableRow.LayoutParams.MATCH_PARENT));
TextView value = new TextView(getActivity());

value.setText(((EditText)view.findViewById(R.id.value)).getText().toString());
value.setTextColor(Color.BLACK);
value.setLayoutParams(new TableRow.LayoutParams(
        0,
        TableRow.LayoutParams.MATCH_PARENT));
TextView type = new TextView(getActivity());

type.setText(((Spinner)view.findViewById(R.id.data_type_spinner)).getSelectedItem().toString());
type.setTextColor(Color.BLACK);
type.setLayoutParams(new TableRow.LayoutParams(
        0,
        TableRow.LayoutParams.MATCH_PARENT));

row.addView(name);
row.addView(value);
row.addView(type);
Mattia Maestrini
  • 32,270
  • 15
  • 87
  • 94
Marcin D
  • 918
  • 7
  • 14

1 Answers1

0

Well, at first glance, the issue looks like your settings are different in the 2 examples. In the xml, you are using layout_width="0dp" and layout_weight="1". However, in the code you are using MATCH_PARENT.

Closest example I could find: How do you setLayoutParams() for an ImageView?

Community
  • 1
  • 1
BigDCoder
  • 45
  • 9
  • I'm reading it and i am already doing what the answer suggests. The code part is partially irrelevant because i want to inject row layout into the table and not build then add the rows in code. (maybe using an adapter?) Unless of course i could achieve the layout through setLayoutParams. – Marcin D May 08 '15 at 18:48
  • I ended up getting the right layout by adding a weight to setLayoutParams – Marcin D May 08 '15 at 19:49