3

Although this looks a lot repeated question but first time for me. I searched all over and could not get the result and ended up posting here.

I am creating a table dynamically of which the TableLayout part is written in xml part.

<RelativeLayout
        android:layout_width="70dp"
        android:layout_height="40dp"
        android:background="@color/colorPrimaryDark"
        android:id="@+id/componentA">
        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:gravity="center"
            android:id="@+id/tl_componentA">


        </TableLayout>

</RelativeLayout>

I created an object for the table

TableLayout tableLayoutA;
tableLayoutA= (TableLayout)view.findViewById(R.id.tl_componentA);

Now I tried to add a row dynamically from here onwards as

createTableRowColumn(tableLayoutA);

Functions Related are

private void createTableRowColumn(TableLayout tableLayout){

        TableRow tableRow1= new TableRow(this.context);
        tableRow1.setBackgroundColor(getResources().getColor(R.color.colorAccent));
        setTableRowColumnProperty(tableRow1);
        tableLayout.addView(tableRow1);
    }

private void setTableRowColumnProperty(TableRow tableRow){

    TableRow.LayoutParams layoutParams= new TableRow.LayoutParams(70, 40);
    tableRow.setLayoutParams(layoutParams);
}

I did all this and nothing showed me in the emulater. But when I gave same structure in xml mannually then thing was working well.

For this reason i tried something to figure out

Toast.makeText(getContext(), tableLayoutA.getHeight()+"", Toast.LENGTH_LONG).show();

In toast it was showing me 0. I could not get why, although I have fixed the size for tableLayoutA in the xml itself.

1 Answers1

1

A table in real life needs at least one row and one column. You didn't see the table, because you only created a row, but, there were no column.

You need to add a column to the row for something to be visible.

Try this :

  TextView label_date = new TextView(this);
    label_date.setText("DATE");
    label_date.setTextColor(Color.WHITE);
    label_date.setPadding(5, 5, 5, 5);
    tableRow.addView(label_date);// add the column to the table row here

    tableLayout.addView(tableRow, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

You can replace the textView with whatever the value you want it to be.

tableLayout.addView(tableRow, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

Here what happens is, you say, add a view into tableLayout and you tell which view it is, and you also say the width and height of the view should wrap it's contents.

Also, specifying 40,70 is not a great idea, what happens when you have varying screen sizes. Furthermore, use a library to handle dynamic view addition and removal. You don't need to reinvent the wheel and save a lot of time and effort. For table views, https://github.com/EsotericSoftware/tablelayout might be a good one (not sure). Another question is, is table view what you are looking for? Make sure you are not mistaking recyclerView for table views, I say this because I don't understand your use case.

Useful link : https://technotzz.wordpress.com/2011/11/04/android-dynamically-add-rows-to-table-layout/

rgv
  • 1,186
  • 1
  • 17
  • 39
  • Thats working. Can you please explain me what had happened. Also explain how does this work --- tableLayout.addView(tableRow, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); – Shubham Agarwal Bhewanewala Mar 07 '17 at 18:46
  • Why didn't you use TableRow.LayoutParams? – Shubham Agarwal Bhewanewala Mar 07 '17 at 19:13
  • I want to fix row size to (70*40)dp how can I do that? – Shubham Agarwal Bhewanewala Mar 08 '17 at 06:07
  • 1) Why didn't you use TableRow.LayoutParams? : Because it didn't seem to work.It might work, I don't know, but what I did, did work. 2) I want to fix row size to (70*40)dp how can I do that? you need to set table width and height as 70*40, set row width and height as match parent and column heigh and width as wrap content(or match parent) and match parent, respectively. – rgv Mar 08 '17 at 16:11
  • 1
    It was not working either way. So insted of half dynamic layout I made it complete dynamic layout and now everything is working which I tried earlier and had not worked then.. :-/ – Shubham Agarwal Bhewanewala Mar 08 '17 at 16:44