TableLayout.addView(view) vs. TableLayout.addView(view, pos) - android

I have two layout files, a main one containing a custom view, and a table, and a row layout.
Depending on circumstances, the table needs additional rows, inflated from the row layout like this:
TableLayout tl = (TableLayout) mParent.findViewById(R.id.table_layout);
LayoutInflater inflater = LayoutInflater.from(mParent);
TableRow tr = (TableRow) inflater.inflate(R.layout.layout_row, null);
tl.addView(tr, 0);
This works as intended.
tl.addView(tr)
This does nothing at all.
Why? I spent half an hour on this, before I tried the addView with two arguments, and I can now make this work, yet it would be better to actually understand why.
Thanks!

Stupid question. It was added, but invisible because out of the screen. Sorry about this.

Related

Adding TableRow Dynamically to TableLayout in xml

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.
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/

How to add several relativeLayout with 2 views inside programmatically?

I took a look on stackoverflow but I can't find the answer.
I would like to add several rows (RelativeLayout) into the parent (LinearLayout). The RelativeLayout would be composed of 2 views, a ImageView on the left and a TextView on its right, both into the same row, for each item:
LinearLayout userLayout = (LinearLayout) view.findViewById(R.id.participant_user);
RelativeLayout rL = new RelativeLayout(context);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_LEFT)
for (Participant participant : participants) {
TextView textView = setTextView(context, participant.getName());
rL.addView(textView, params);
ImageView imageView = new ImageView(context);
imageView.setImageResource(R.drawable.bz_ic_default_user);
rL.addView(imageView, params);
userLayout.addView(rL);
}
It doesn't work, the first element is only displayed or the app crashed...
Thank you for your help !
You should probably use a ListAdapater or RecylerView here
https://developer.android.com/training/material/lists-cards.html
http://developer.android.com/reference/android/widget/ListAdapter.html
You are trying to create a list of items with an unknown #. The way you are doing it, performance will be bad and you are inflating views into a linearlayout and not reusing views.
You are creating a RelativeLayout, filling it n times (one per participant) and trying to add it n times. At the very least you should create n different RelativeLayouts.
Anyway, as a rule of thumb, every time you have to crete a list ask yourself: will it contain 10 elements or 1000. If it's 10, then you can follow your approach (but I would advise to create every row inflating an xml instead od instantiating the views), if it's 1000 use a ListView (or some kind of RecyclerView), much simpler and efficient.
As the previous answers stated, using a ListAdapter would probably make things work. Here is an example.
If you really want to do it your way, you should share your error log (LogCat output).
As everyone here said, It's better to use the RecyclerView or the ListView. But if you want do it in this way for another reason, then I think that the best that you can do is:
LinearLayout mainLayout = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
mainLayout.setLayoutParams(params);
LayoutInflater inflater = LayoutInflater.from(this);
for (Participant participant : participants) {
View rowView = inflater.inflate(R.layout.row, mainLayout, false);
((TextView) rowView.findViewById(R.id.name)).setText(participant.name);
((ImageView) rowView.findViewById(R.id.photo)).setImageResource(R.drawable.photo); //BTW You can set it in the xml and avoid this
mainLayout.addView(rowView);
}
setContentView(mainLayout);

android tablerow and columns in code

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

Setting number of columns programmatically in TableLayout

I have an XML layout which contains a TableLayout with an unknown number of TableRows...
The number of Rows will be established durin runtime, what I do know though is that I want two columns...
So I have a couple of questions regarding this :
- is there a way to set the whole TableLayout to have 2 columns ?
- is there a way programmatically to give an id to the (during runtime) created TableRows which will be placed within the TableLayout, so I can reference them later on from other parts of the software ?
You can build your table rows via XML parts and LayoutInflater. Say you had this as your table_cell.xml:
<TextView android:id="#+id/text"
android:text="woot" />
And this as your table_row.xml (unless you're doing something fancy with your TableRow, you may not need to put it in it's own XML file, and instead just create it programmatically. The result will be the same):
<TableRow />
Assuming your TableLayout reference was called "table", you could do something like this:
TableLayout table = (TableLayout)findViewById(R.id.table);
LayoutInflater inflater = getLayoutInflater();
for(int i = 0; i < 5; i++) {
TableRow row = (TableRow)inflater.inflate(R.layout.table_row, table, false);
View v = (View)inflater.inflate(R.layout.table_cell, row, false);
row.addView(v);
v = (View)inflater.inflate(R.layout.table_cell, row, false);
row.addView(v);
// you can store your reference to `row` here for later use
table.addView(row);
}
With this technique, you can still set up your layout in XML, making it easier to read/organize/edit, and you still have programmatic control over how many columns and rows are in the table. You can also store references to each table row for later use.

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