Chosen1
Chosen1

Reputation: 269

Android - TableRow column alignment programmatically

I have been stuck on this issue for hours and I have no idea how to get passed it.

I have programatically created a table (no problem), but this is the issue I am having. When I create the row, I would then like to create two separate columns. As you'll see in my example, I have two different TextViews on the same line ('Type' and 'Number'). As it stands, both of them are shifted over to the left.

What I would like is for them to be in two separate columns, equally split apart centred in each column.

Here is the relevant code to what I am trying to accomplish.

TableLayout serviceLineLayout = new TableLayout(getContext());
        serviceLineLayout.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));

TableRow firstTableRow = getFirstTableRow(getContext());
        serviceLineLayout.addView(firstTableRow);

private TableRow getFirstTableRow(Context context) {
        TableRow tableRow = new TableRow(context);

        tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT));


        TextView typeHeader = new TextView(context);
        typeHeader.setId(0);
        typeHeader.setText("Type");
        typeHeader.setTextSize(20);
        tableRow.addView(typeHeader);

        TextView numberHeader = new TextView(context);
        numberHeader.setId(0);
        numberHeader.setText("Number");
        numberHeader.setTextSize(20);
        numberHeader.setGravity(Gravity.RIGHT);
        tableRow.addView(numberHeader);

        return tableRow;
    }

Upvotes: 0

Views: 1345

Answers (1)

Sreehari
Sreehari

Reputation: 5655

For this tablelayout defined in xml, following will be the dynamic code in corresponding java file (Activity/Fragment).

<TableLayout
            android:id="@+id/fenceTableLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
</TableLayout>

Activity logic for TableView

//Table Layout parameters
        TableRow.LayoutParams textViewParam = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,1.0f);
        TableLayout tableLayout = (TableLayout) view.findViewById(R.id.fenceTableLayout);
        TableRow trHead = new TableRow(context);
        LayoutParams tableRowParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        trHead.setLayoutParams(tableRowParams);

        TextView nameHead = new TextView(context);
        nameHead.setText("Content left");
        nameHead.setLayoutParams(textViewParam);
        nameHead.setGravity(Gravity.CENTER);
        trHead.addView(nameHead);

        TextView detailHead = new TextView(context);
        detailHead.setText("Content right");
        detailHead.setGravity(Gravity.CENTER);
        detailHead.setLayoutParams(textViewParam);

        trHead.addView(detailHead);
        tableLayout.addView(trHead);

NOTE: Many values have been referred from resource files, You can either neglect/replicate those.

Upvotes: 1

Related Questions