Sheppard25
Sheppard25

Reputation: 598

How to set TableRow width to max in Android?

I have a table defined in .xml file but rows and headers have to be added dynamically. I have a problem with fill width with table row on horizontal and vertical orientation. I have tried stretchColumns parameter but it did not help. What is the best way to do it?

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

        TableRow headerRow  = new TableRow(this);

        JSONObject obj;

        obj = new JSONObject(loadJSONFromAsset());

        JSONArray records = obj.getJSONArray("tests");

        ArrayList<String> headers = new ArrayList<>();
        headers.add("Header 1");
        headers.add("Header 2");
        headers.add("Header 3");
        headers.add("Header 4");
        headers.add("Header 5");

        for(int i = 0; i < headers.size(); i++) {
            TextView header = new TextView(this);
            header.setText(headers.get(i));
            header.setTextColor(Color.WHITE);
            header.setBackgroundColor(Color.rgb(24, 116, 205));
            header.setPadding(15, 15, 15, 15);
            header.setTextSize(20);
            header.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);

            header.setMaxLines(1);
            headerRow.addView(header);

        }

        table.addView(headerRow);

and xml

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content">


<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <HorizontalScrollView
            android:layout_height="wrap_content"
            android:layout_width="match_parent">

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

            </TableLayout>
        </HorizontalScrollView>
    </ScrollView>

</LinearLayout>

</GridLayout>

Upvotes: 3

Views: 3312

Answers (1)

Sreehari
Sreehari

Reputation: 5655

Try this way. The following will make sure TableRow is aligned to full available width.

After base components, put your TableLayout in XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/mainLL"
    android:orientation="vertical">
<TableLayout
            android:id="@+id/tableLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
</LinearLayout>

Now in your logic, you can add according to the number of items from response. This should be in a for loop.

for(int i = 0; i < leftComponent.size(); i++) 
    {
            //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(leftComponent[i]);
            nameHead.setLayoutParams(textViewParam);
            nameHead.setGravity(Gravity.CENTER);
            trHead.addView(nameHead);

            TextView detailHead = new TextView(context);
            detailHead.setText(rightComponent[i]);
            detailHead.setGravity(Gravity.CENTER);
            detailHead.setLayoutParams(textViewParam);

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

    }

You may need to customize according to your requirement.

Upvotes: 5

Related Questions