user468311
user468311

Reputation:

Custom TableLayout with multiple TextViews in rows

I want to crate custom TableLayout with rows like this:

TV is for TextView, i.e. I want to add 11 TextViews to the row:

enter image description here

Each row begins with a title and then I add 5 pairs of TextViews, so that table row is as wide, as screen is. Here's my code:

public class FlowTable extends TableLayout {

    private Context context;

    public FlowTable(Context context) {
        super(context);
        this.context = context;
    }

    public FlowTable(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public void addContent(List<ResultItem> data) {

        TableRow tableRow = new TableRow(context);

        LayoutParams params = new LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1);

        for (int i = 0; i < data.size(); i++) {

            if (i % 5 == 0) {
                this.addView(tableRow, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

                tableRow = new TableRow(context);
                TextView tvRange = new TextView(context);
                tvRange.setLayoutParams(params);
                tvRange.setText(genRange(i+1));
                tableRow.addView(tvRange);

            }
            TextView tvDistance = new TextView(context);
            tvDistance.setLayoutParams(params);
            tvDistance.setText(String.valueOf(data.get(i).distance));

            TextView tvResult = new TextView(context);
            tvResult.setLayoutParams(params);
            tvResult.setText(data.get(i).result);

            tableRow.addView(tvDistance);
            tableRow.addView(tvResult);
        }
    }

    private String genRange(int currIndex){
        /********************/
        return somestring;
    }
}

Using table:

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

    <packagename.FlowTable
        android:id="@+id/flowTable"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</RelativeLayout>

In fragment:

View root = inflater.inflate(R.layout.fragment_session_summary, container, false);
        FlowTable flowTable = (FlowTable)root.findViewById(R.id.flowTable);
        flowTable.addContent(data);

The problem: the screen is just empty! Nothing at all. Before I added the layout params to textview it worked, but row didn't occupy the screen width. My initial solution was based on the LinearLayout samples, because TableRow is an extention of LinearLayout. But I can't make it work. Thanks.

Upvotes: 4

Views: 9224

Answers (2)

Shreyash Mahajan
Shreyash Mahajan

Reputation: 23596

If you want to use the xml Layout for that then try this:

    <TableLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="wrap_content" android:layout_height="wrap_content">
                <TableRow android:weightSum="1">
                    <LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
                        android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="4dp">
                        <TextView android:id="@+id/totalText"
                            android:layout_height="wrap_content" android:layout_width="wrap_content"
                            android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
                            android:text="Title" android:layout_weight="1"/>
                    </LinearLayout>
                    <!-- First Group -->
                    <LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
                        android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="4dp">

                        <TextView android:id="@+id/totalText"
                                android:layout_height="wrap_content" android:layout_width="wrap_content"
                                android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
                                android:text="Tv1"/>
                        <TextView android:id="@+id/totalText"
                                android:layout_height="wrap_content" android:layout_width="wrap_content"
                                android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
                                android:text="Tv2"/>
                    </LinearLayout>
                    <!-- Second Group -->
                    <LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
                        android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="4dp">
                        <TextView android:id="@+id/totalText" 
                                android:layout_height="wrap_content" android:layout_width="wrap_content"
                                android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
                                android:text="Tv1"/>
                        <TextView android:id="@+id/totalText" 
                                android:layout_height="wrap_content" android:layout_width="wrap_content"
                                android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
                                android:text="Tv2"/>
                    </LinearLayout>


                    <!-- Third Group -->
                    <LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
                        android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="4dp">
                        <TextView android:id="@+id/totalText" 
                                android:layout_height="wrap_content" android:layout_width="wrap_content"
                                android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
                                android:text="Tv1"/>
                        <TextView android:id="@+id/totalText" 
                                android:layout_height="wrap_content" android:layout_width="wrap_content"
                                android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
                                android:text="Tv2"/>
                    </LinearLayout>

                    <!-- Fourth Group -->
                    <LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
                        android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="4dp">

                        <TextView android:id="@+id/totalText"
                                android:layout_height="wrap_content" android:layout_width="wrap_content"
                                android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
                                android:text="Tv1"/>
                        <TextView android:id="@+id/totalText"
                                android:layout_height="wrap_content" android:layout_width="wrap_content"
                                android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
                                android:text="Tv2" />
                    </LinearLayout>
                    <!-- Fifth Group -->
                    <LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
                        android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="2dp">

                        <TextView android:id="@+id/totalText" 
                                android:layout_height="wrap_content" android:layout_width="wrap_content"
                                android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
                                android:text="Tv1"/>
                        <TextView android:id="@+id/totalText" 
                                android:layout_height="wrap_content" android:layout_width="wrap_content"
                                android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
                                android:text="Tv2"/>
                    </LinearLayout>
                </TableRow>
                <TableRow android:weightSum="1">
                    <LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
                        android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="4dp">
                        <TextView android:id="@+id/totalText"
                            android:layout_height="wrap_content" android:layout_width="wrap_content"
                            android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
                            android:text="Title" android:layout_weight="1"/>
                    </LinearLayout>
                    <!-- First Group -->
                    <LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
                        android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="4dp">

                        <TextView android:id="@+id/totalText"
                                android:layout_height="wrap_content" android:layout_width="wrap_content"
                                android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
                                android:text="Tv1"/>
                        <TextView android:id="@+id/totalText"
                                android:layout_height="wrap_content" android:layout_width="wrap_content"
                                android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
                                android:text="Tv2"/>
                    </LinearLayout>
                    <!-- Second Group -->
                    <LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
                        android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="4dp">
                        <TextView android:id="@+id/totalText" 
                                android:layout_height="wrap_content" android:layout_width="wrap_content"
                                android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
                                android:text="Tv1"/>
                        <TextView android:id="@+id/totalText" 
                                android:layout_height="wrap_content" android:layout_width="wrap_content"
                                android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
                                android:text="Tv2"/>
                    </LinearLayout>


                    <!-- Third Group -->
                    <LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
                        android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="4dp">
                        <TextView android:id="@+id/totalText" 
                                android:layout_height="wrap_content" android:layout_width="wrap_content"
                                android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
                                android:text="Tv1"/>
                        <TextView android:id="@+id/totalText" 
                                android:layout_height="wrap_content" android:layout_width="wrap_content"
                                android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
                                android:text="Tv2"/>
                    </LinearLayout>

                    <!-- Fourth Group -->
                    <LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
                        android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="4dp">

                        <TextView android:id="@+id/totalText"
                                android:layout_height="wrap_content" android:layout_width="wrap_content"
                                android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
                                android:text="Tv1"/>
                        <TextView android:id="@+id/totalText"
                                android:layout_height="wrap_content" android:layout_width="wrap_content"
                                android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
                                android:text="Tv2" />
                    </LinearLayout>
                    <!-- Fifth Group -->
                    <LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
                        android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="2dp">

                        <TextView android:id="@+id/totalText" 
                                android:layout_height="wrap_content" android:layout_width="wrap_content"
                                android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
                                android:text="Tv1"/>
                        <TextView android:id="@+id/totalText" 
                                android:layout_height="wrap_content" android:layout_width="wrap_content"
                                android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
                                android:text="Tv2"/>
                    </LinearLayout>
                </TableRow>
</TableLayout>

Hope it will help you. . . And if you want other then xml then let me know. Thanks.

Upvotes: 1

dstricks
dstricks

Reputation: 1485

Try programmatically setting all columns to stretch (didn't seem to work in XML for me):

...
flowTable.addContent(data);
flowTable.setStretchAllColumns(true);

Some other quick facts:

  • No need to try and specify height and width for TableRow within TableLayout because it will always be height=WRAP_CONTENT and width=MATCH_PARENT. See TableLayout documentation where this is listed in the Class Overview section
  • No need to try and specify height and widget for children of TableRow because they will always be height=WRAP_CONTENT and width=MATCH_PARENT. See TableRow documentation where this is listed in the Class Overview section

Might I also humbly suggest a bit of refactoring:

public class FlowTable extends TableLayout {
    private TableRow mCurrentRow;

    public FlowTable(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public FlowTable(Context context) {
        super(context);
        init();
    }

    private void init() {
        mCurrentRow = new TableRow(getContext());
        mCurrentRow.addView(createAndFillTextView("0")); // title for first row
        setStretchAllColumns(true);
    }

    public void addContent(List<ResultInfo> data) {
        for (int i = 0; i < data.size(); i++) {
            if ((i % 5 == 0) && (i != 0) /** Don't do this on 0! */) {
                finishRowAndStartNew(i);
            }

            mCurrentRow.addView(createAndFillTextView(data.get(i).distance));
            mCurrentRow.addView(createAndFillTextView(data.get(i).result));
        }
    }

    private void finishRowAndStartNew(int newRowIndex) {
        addView(mCurrentRow);
        mCurrentRow = new TableRow(getContext());
        mCurrentRow.addView(createAndFillTextView(genRange(newRowIndex+1)));
    }

    private String genRange(int currIndex){
        /********************/
        return String.valueOf(currIndex);
    }

    private TextView createAndFillTextView(String text) {
        TextView tv = new TextView(getContext());
        tv.setText(text);        
        return tv;
    }
}

Upvotes: 10

Related Questions