android tablerow and columns in code - android

I'm trying to create a TableLayout with dynamically added rows and columns (from an array of obbjects). Since it's dynamic must I code(?)
All the examples I can find is doing it using xml layouts. The only ones out there that does it in code only adds a single view, like a button, which makes me believe its not doable, and I need two columns at least, where one of the will contain a view with an image and text.

Try to create table and row doing something like this
TableLayout t=new TableLayout(this);
TableRow tr=new TableRow(this);
and add row with table by
t.addView(tr);
similarly you can add other view (text, image view etc.) by using addView() function

Related

DataGrid Android Studio horizontal vertical scroll

I am looking for solution where could dispay large data with horizontal and vetical scroll. Something like this https://www.syncfusion.com/blogs/post/new-datagrid-for-xamarinios-and-xamarinandroid.aspx. I would like to change number of columns and change the cell values.
Do you have please something like this? When it be a free i will be really happy. Thank you
You can create it by custom TableLayout. And the good news is you can add both side scrolling mechanism in TableLayout. You have to first set a TableLayout xml. For both side scrolling you have to add first ScrollView then HorizontalScrlollView then your TableLayout.
Now you you can create row and column. You can create it dynamically because you (may be) dont know how many column has required. So you have to create custom TableRow and cell and you can easily add your tableLayout .
TableRow row= new TableRow(this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(lp);
// then create your text view and add it row. and last add row in your table layout
This is just example. You can create your own data grid library
And you can some ready mate library to show data grid in table view like your reference. :
https://github.com/evrencoskun/TableView
https://github.com/ISchwarz23/SortableTableView

TableLayout row strech/shrink

I'm trying to fit a 20x20 table into a view using a programmatically built TableLayout. The setStretchAllColumns/setShrinkAllColumns methods work perfectly for squeezing all the columns in, but I haven't found a way to get all the rows in.
Is there a corresponding method for packing the rows in?
There aren't any setStretchAllColumns/setShrinkAllColumns for rows in a TableLayout. One thing that you could try(I haven't tested it) is to give your TableLayout a weigtSum of 20(with the method setWeightSum()) and then set the weight(the layout_weight attribute from a xml layout) of 1 for each of your 20 TableRows in the TableLayout.

Updating some rows dynamically in TableLayout

I'm adding rows to my TableLayout dynamically and each row has more than one TextView.
I want to loop for each row in my TableLayout and I want to update some rows (not every row), for example the text of TextView in my rows. How can I manage this?
Loop through the children of the TableLayout, they should be TableRows (or another layout) - skip the ones you don't want to update. Loop through or findViewById the children of the TableRow (or whatever) which will be your TextViews
When you add the TextView / CheckBox to the TableRow, you can set the View's id with setId(R.id.id_of_the_view) (which you might want to add to the res/values/ids.xml file).
Then loop through like so:
TableLayout tableLayout = null;
for(int n = 0, s = tableLayout.getChildCount(); n < s; ++n) {
TableRow row = (TableRow)tableLayout.getChildAt(n);
TextView name = (TextView)row.findViewById(R.id.tv_name);
}
since you're using row.findViewById, you're looking for a specific id inside a specific row.
I recommend using the setTag method to quick reach specific rows, and Views within rows.
For example, lets say you have a CheckBox in a certain column, which represents "isSomethingEnabled". When creating the CheckBox, do setTag("foo").
Use setTag on the row as well, such as setTag("rowKey")
To quickly get to a specific row
TableRow tRow = (TableRow) tLayout.findViewWithTag("rowKey");
And to quickly get to a specific child
View v = tRow.findViewWithTag("foo");

Can we create tables with borders dynamically in Android

Iam trying to create a table with borders using TableLayout. If I add static rows in xml file i am able to see the borders. But the problem arises if I try to add some dynamic rows. Data for the table comes dynamically. Can anyone help in solving this problem. If possible please provide the source code(.java file and .xml file).
You can create a rectangle image to use as a background for the row. TableRow extends View, so this should work (untested).
TableRow row = new TableRow (this);
row.setBackgroundResource (android.R.drawable.edit_text);
// rest of your code
If it doesn't work, the (tested) solution is to add a color background to the table and padding to the row.

How can I find out the row /column of a view inside table layout

Suppose I have a view inside TableLayout, like this:
TableLayout tableLayout;
View view = (View)tableLayout.findViewById(R.id.control);
How can I find out the view's row/column within the TableLayout?
A view that is within a TableRow should have TableRow.LayoutParams as its LayoutParams. From that you can get the layout_column.
The row seems to be a bit harder. I'd expect the TableLayout.LayoutParams (gotten from the TableRow) to have that information, but it doesn't seem to. You could try using tableLayout.getIndexOfChild(rowView).
There is no such getIndexOfChild(view) in TableLayout class, but a indexOfChild()!
While designing the tablerow in code, you can use setTag() to set it to row number. Later it can be retrieved by tableRow.getTag(). In xml you may use android:tag="2".

Categories

Resources