Android: Tablerows not matching parent width

I have a scrolling tablelayout that's supposed to display user progress on various levels in the game. Everything works except that the row widths appear to be wrap content despite the xml width being set to match_parent. I need the table data to be added dynamically. Here's my code for adding the rows to the tableView:

    public View getView(LayoutInflater inflater, Context context, int position) {
        View view = inflater.inflate(R.layout.prog_cell_layout, null);

        String letter = " " + (char)('a'  + position) + " ";
        TextView tv = (TextView)view.findViewById(R.id.progLetterName);
        tv.setText(letter);

        return view;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_progress, container, false);

        //It starts here. There are 26 rows
        TableLayout tab = (TableLayout)rootView.findViewById(R.id.tableView);

        for (int i = 0; i < 26; i++) {

            View conView = getView(inflater, getContext(), i);


            TableRow.LayoutParams lp = new TableRow.LayoutParams(
                    TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT
            );
            conView.setLayoutParams(lp);

            tab.addView(conView);

        }

        return rootView;
    }

The methods are found within a fragment class acting as the table's container. I know the table itself is the right width, but the rows are not.

The rows are read from an xml layout:

<?xml version="1.0" encoding="utf-8"?>
<TableRow android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_green_dark"
        >

        <TextView
            android:id="@+id/progLetterName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/black"

            android:layout_marginLeft="10dp"
            android:text="a"
            android:textSize="20dp"
            android:fontFamily="sans-serif-light"
            android:textAlignment="center"
            />

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/black"
            android:text="TextView"
            android:textSize="20sp"
            android:fontFamily="sans-serif-light"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"/>
    </RelativeLayout>
</TableRow>

I've tried a lot of things...But obviously none have been able to make the TableRow's to match the parent's width.

1 answer

  • answered 2018-07-11 02:44 Psypher

    You need to add the TableRow within a TableLayout container and not without that. Change your layout file as below, add the TableLayout as the parent layout and within the TableLayout each row should correspond to You do not need to have android:orientation="horizontal"for a RelativeLayout, thats applicable for layouts like LinearLayout

    <TableLayout android:layout_width="match_parent"
        android:layout_height="wrap_content"
        xmlns:android="http://schemas.android.com/apk/res/android">
    
        <TableRow android:layout_width="match_parent"
            android:layout_height="wrap_content">
        <RelativeLayout 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_green_dark">
           ............