Search code examples
androidandroid-layoutandroid-tablelayout

Android: Views in TableLayout added programatically not showing


I'm creating an app and I need to have dynamic tables inside it so I tried to do so and for the most part, it works. But I have these dividers between my rows and columns, I used Views to do so and they come out like this:

EDIT: I Think the views are just not being shown and my first column is where the left border should be, don't know what is doing this though.

Dividers appear in the title row

This is the code I use to generate the rows:

private void generateMaterialRows(){
        TableLayout tableMaterials = (TableLayout) findViewById(R.id.info_table_materials);
            //create counter for index in table
            int c = 3;
        for(Material m: materials){


            //makes Rows
            TableRow tRow = new TableRow(this);
            TableRow tDivider = new TableRow(this);

            //Makes 5 bottomBorders and puts them in an Array
            View[] bottomBorders = new View[5];

            for (int i = 0; i < 5; i++) {
                View bottomBorder = new View(this);
                bottomBorder.setBackgroundResource(R.color.colorDarkGray);
                int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 3, getResources().getDisplayMetrics()); //converts dp to px
                bottomBorder.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height));
                bottomBorder.setLayoutParams(bottomBorder.getLayoutParams());

                bottomBorders[i] = bottomBorder;
            }

            //makes 3 sideBorders and puts them in an Array
            View[] sideBorders = new View[3];

            for (int i = 0; i < 3; i++){
                View sideBorder = new View(this);
                sideBorder.setBackgroundResource(R.color.colorDarkGray);
                int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 3, getResources().getDisplayMetrics()); //converts dp to px
                sideBorder.setLayoutParams(new ViewGroup.LayoutParams(width, ViewGroup.LayoutParams.MATCH_PARENT));
                sideBorder.setLayoutParams(sideBorder.getLayoutParams());

                sideBorders[i] = sideBorder;
            }

            //makes columnAmount
            TextView txtAmount = new TextView(this);
            txtAmount.setBackgroundResource(R.color.colorBlueLight);
            txtAmount.setPadding(5,5,5,5);
            txtAmount.setText(String.valueOf(m.getAmount()));

            //makes columnName
            TextView txtName = new TextView(this);
            txtName.setBackgroundResource(R.color.colorBlueLight);
            txtName.setPadding(5, 5, 5, 5);
            txtName.setText(String.valueOf(m.getName()));

            //puts content in TableRow
            tRow.addView(sideBorders[0]);
            tRow.addView(txtAmount);
            tRow.addView(sideBorders[1]);
            tRow.addView(txtName);
            tRow.addView(sideBorders[2]);

            //makes divider
            for (int i = 0; i < 5; i++){
                tDivider.addView(bottomBorders[i]);
            }

            tableMaterials.addView(tRow, c);
            tableMaterials.addView(tDivider, c+1);

            c++;
        }
    }

This is the TableLayout in the XML:

                <TableLayout
                    android:id="@+id/info_table_materials"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="@dimen/activity_vertical_margin">
                    <!-- Divider Above Title Row -->
                    <TableRow>
                        <View
                            android:background="@color/colorDarkGray"
                            android:layout_height="3px"/>
                        <View
                            android:background="@color/colorDarkGray"
                            android:layout_height="3px"/>
                        <View
                            android:background="@color/colorDarkGray"
                            android:layout_height="3px"/>
                        <View
                            android:background="@color/colorDarkGray"
                            android:layout_height="3px"/>
                        <View
                            android:background="@color/colorDarkGray"
                            android:layout_height="3px"/>
                    </TableRow>

                    <!-- Title Row -->
                    <TableRow>
                        <View
                            android:background="@color/colorDarkGray"
                            android:layout_width="3px"
                            android:layout_height="fill_parent"/>
                        <TextView
                            android:text="@string/info_amount"
                            android:background="@color/colorLightGray"
                            android:paddingRight="10dp"
                            android:paddingLeft="10dp"
                            android:paddingTop="5dp"
                            android:paddingBottom="5dp"/>
                        <View
                            android:background="@color/colorDarkGray"
                            android:layout_width="3px"
                            android:layout_height="fill_parent"/>
                        <TextView
                            android:text="@string/info_materials"
                            android:background="@color/colorLightGray"
                            android:paddingRight="10dp"
                            android:paddingLeft="10dp"
                            android:paddingTop="5dp"
                            android:paddingBottom="5dp"/>
                        <View
                            android:background="@color/colorDarkGray"
                            android:layout_width="3px"
                            android:layout_height="fill_parent"/>
                    </TableRow>

                    <!-- Divider Between Title and Content Rows -->
                    <TableRow>
                        <View
                            android:background="@color/colorDarkGray"
                            android:layout_height="3px"/>
                        <View
                            android:background="@color/colorDarkGray"
                            android:layout_height="3px"/>
                        <View
                            android:background="@color/colorDarkGray"
                            android:layout_height="3px"/>
                        <View
                            android:background="@color/colorDarkGray"
                            android:layout_height="3px"/>
                        <View
                            android:background="@color/colorDarkGray"
                            android:layout_height="3px"/>
                    </TableRow>

                </TableLayout>

I'm still fairly new at android so I'm still learning but this one got me real confused and I can't seem to find what's wrong.


Solution

  • I found the problem. I had no clue how to use the LayoutParams so I used ViewGroup.LayoutParams but since they are in a TableRow I should have used TableRow.LayoutParams