Dynamically load customized table cells in table - android

I want to create say 5 different types of cells in table along with identifiers and load them appropriately as per the given data depending upon the type?
Creating TableRow inside TableLayout seems to be one of the options but how to dynamically create the tableRows depending upon the type?
Thanx in advance.

Can you detect the type on execution time? If yes, it should be straight forward using a switch or an if else structure.
To inflate an XML resource on run time depending on the row type use:
((LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(layoutId, yourTableLayout, true);
Set the appropiate layoutId before inflating the resource and then proceed. The parameters yourTableLayout and true are just my guess, check the documentation at LayoutInflater and choose the inflate method that fits yout needs.
To create TableRows dynamically, this tutorial may help: Creating TableRow rows inside a TableLayout programatically
Basically:
1- Fetch TableLayout and create TableRow
// Get the TableLayout
TableLayout tl = (TableLayout) findViewById(R.id.maintable);
TableRow tr = new TableRow(this);
tr.setId(100+current);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
2- Create elements to add
TextView labelTV = new TextView(this);
labelTV.setId(200+current);
labelTV.setText(provinces[current]);
labelTV.setTextColor(Color.BLACK);
labelTV.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(labelTV);
3- Add the TableRow to the TableLayout
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
Seems to be easy and fast, I haven't tested it tho.

Related

Resizing Button in a TableLayout, Dynamically (Android)

Alright, I have searched for its solution But unable to find it. The Question is simple. There is a TableLayout in Android, I am adding few Rows in it. In the Last Row, I am adding a button. Now, the problem is the size of the button is not constant. What I mean is that, it fills the column. If the above data takes longer width of the column, the width of the button, accordingly increases. I have tried LayoutParams, setHeight, setWidth function too, but that didn't help me..
I would appreciate if someone would help me out.
TableLayout table=new TableLayout(this);
TableRow tableRow=new TableRow(this);
Button button=new Button(this);
TableRow.LayoutParams trParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
Button.setLayoutParams(trParams);
tableRow.AddView(button);
table.addView(tableRow);
You have called setLayoutParams on Button not button.
Names are case sensitive. Button is the class while button is your object.
Try:
TableLayout table=new TableLayout(this);
TableRow tableRow=new TableRow(this);
Button button=new Button(this);
TableRow.LayoutParams trParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
tableRow.AddView(button, trParams);
table.addView(tableRow);
However this will not fix the main issue. According to the Reference docs all children of a TableRow are forced to MATCH_PARENT for their width.
You could try nesting your button inside another layout. For example put your button inside a horizontal LinearLayout, then put the LinearLayout into your TableRow.

Layouts - MATCH_PARENT hides elements

I'm preparing layout for list of countries shown in categories. I have problem formatting my layout. I want category titles (bigger ones) to be "100% width", so far they are WRAP_CONTENT formatted (blue colour is for testing purposes):
I used the following code:
TableLayout table = new TableLayout(this);
table.setPadding(24, 14, 24, 24);
table.setStretchAllColumns(true);
table.setShrinkAllColumns(true);
// ...
TableRow titleRow = new TableRow(this);
titleRow.setGravity(Gravity.CENTER_HORIZONTAL);
titleRow.setPadding(0, 18, 0, 8);
titleRow.setBackgroundColor(Color.BLUE);
TextView title = new TextView(this);
title.setText("TEST TITLE"); //title.setText(currentContinent);
title.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
title.setGravity(Gravity.CENTER);
title.setBackgroundColor(Color.DKGRAY);
title.setPadding(0, 6, 0, 8);
titleRow.addView(title);
table.addView(titleRow, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
I changed last part of code to make TextView filling parent (TableRow):
titleRow.addView(title, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
table.addView(titleRow, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
But then my TextView dissapears!
Debug view http://www.wysyper.pl/u/4417tahfcvnb.jpeg
What can cause the problem?
I trust that you have a good reason not to use XML layouts for this, which should make your life a lot less code-filled and eliminate some potential bugs.
Have you tried it using instances of the proper LayoutParams subclass for your parent views? Each view added to a TableLayout should use instances of TableLayout.LayoutParams. Likewise TableRow expects instances of TableRow.LayoutParams.
If that doesn't fix it, I recommend building the layout in XML instead of code.

Android: Tablerow multiline textview clipping vertically

I am dynamically adding rows with two columns to a table. One of the columns holds text that will spread across multiple lines. I've added the weight on the layout parameters for the textview so it no longer clipps outside of the screen, but it seems to be restricted to two and a half lines showing in the table, regardless of how long the multiline text is. The table is just defined in xml and everything else is done programmatically.
TableRow.LayoutParams rowParams = new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams paramsStar = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
paramsStar.setMargins(0,0,40,0);
TableRow.LayoutParams paramsText = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.FILL_PARENT, (float)1.0);
TableRow tr = new TableRow(this);
tr.setLayoutParams(rowParams);
TextView viewStar = new TextView(this);
viewStar.setTextSize((float) 30.0);
viewStar.setText("*");
viewStar.setLayoutParams(paramsStar);
viewStar.setTypeface(Typeface.DEFAULT_BOLD, 2);
TextView viewText = new TextView(this);
viewText.setText("Long multiline text piece"); //<--- Here the long text goes
viewText.setLayoutParams(paramsText);
tr.addView(viewStar);
tr.addView(viewText);
table.addView(tr, rowParams);
How do i fix this vertical clipping?
Screenshot of the problem below:
Here is the link showing the problem
I've found the root of the problem. The first column values textsize made the multiline text clip. If i reduce the textsize, the problem disappears.
I cant explain why though.
Another answer I'll drop in here: Android measures table row height in a very non-intuitive way. If you have 2 TextView(s), one for each column of a TableRow, you need to ensure that whatever top and bottom padding you've set for the first TextView is set identically for the 2nd TextView or you will get strange things like content clipping and weird positioning of the 2nd TextView.

How can I add separating lines between my TableRows that are created programmatically?

I have a TableLayout that is created programmatically in an Android project. I keep adding TableRows as long as there are more rows fetched from the database. Now I want to add separating lines, like a border, between the TableRows.
In my other TableLayout that I created statically from XML I used a View as a separator, style with a style.xml.
I tried adding a View to the tablelayout like so:
View v=new View(this);
v.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
v.setBackgroundResource(R.drawable.rowseparator_shape);
tr.addView(mTvDate);
tr.addView(mTvResult);
tl.addView(tr);
tl.addView(v);
But it only gets added once after all the collected TableRows. What would be a smart way of adding one View for each tr added? Or should I use something else alltogether?
View v = new View(this);
v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
v.setBackgroundColor(Color.rgb(51, 51, 51));
tr.addView(mTvDate);
tr.addView(mTvResult);
tl.addView(tr);
tl.addView(v);
Here I'm creating a view that is one pixel high with a specific background color. This works for me.
Thanks to Madhusuthanan for this. I spent a while searching for how to do this to simply separate TextViews with a horizontal line. I was creating my view programmatically (without using a Table layout). Here is what I came up with based on the above answer:
View line = new View(this);
line.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 1));
line.setBackgroundColor(Color.rgb(51, 51, 51));
layout.addView(line);
Simple! Hope this helps someone else!
You can use Listview that will be easiler and better than doing this.

android: two issues using Tablerow+TextView in Tablelayout

I am using Tablerow+TextView to make a simple view for blog posts and their replies. In each TableRow I put a TextView in. Now I have two issues:
The text which is longer than the screen won't automatically wrap up to be multi-line. Is it by design of TableRow? I've already set tr_content.setSingleLine(false); [update] This has been addressed, I think I should change Fill_parent to be Wrap_content in textView.tr_author_time.setLayoutParams(new LayoutParams(
LayoutParams.**WRAP_CONTENT**,
LayoutParams.WRAP_CONTENT));
The Table won't scroll like ListView. My rows are more than the screen size. I expect the table could be scrolled down for viewing just like ListView. Is that possible?
Here is my code:
TableLayout tl = (TableLayout) findViewById(R.id.article_content_table);
TextView tr_title = new TextView(this);
TextView tr_author_time = new TextView(this);
TextView tr_content = new TextView(this);
TableRow tr = new TableRow(this);
for(int i = 0; i < BlogPost.size(); i++){
try{
// add the author, time
tr = new TableRow(this);
/////////////////add author+time row
BlogPost article = mBlogPost.get(i);
tr_author_time = new TextView(this);
tr_author_time.setText(article.author+"("+
article.post_time+")");
tr_author_time.setTextColor(getResources().getColor(R.color.black));
tr_author_time.setGravity(0x03);
tr_author_time.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(tr_author_time);
tl.addView(tr,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
////////////////////// then add content row
tr = new TableRow(this);
tr_content = new TextView(this);
tr_content.setText(article.content);
tr_content.setSingleLine(false);
tr_content.setGravity(0x03);
tr_content.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(tr_content);
tr.setBackgroundResource(R.color.white);
tl.addView(tr,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
A more appropriate thing to do for wrapping items would have been to add android:shrinkColumns="*" or android:shrinkColumns="1" to the TableLayout, this would probably have fixed the wrapping issue.
For Details
This isn't really a complete answer, but it really seems like you're doing this the hard way.
Instead of constructing your TableRows manually, you should set them up in xml like this:
tablerow.xml:
<TableRow xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:id="#+id/content"
android:singleLine="false"
android:textAppearance="#style/someappearance" />
</TableRow>
Prior to your loop, get a reference to a LayoutInflater:
LayoutInflater inflater = getLayoutInflater();
Then, inside your loop, create an instance of tablerow using the LayoutInflater:
TableRow row = (TableRow)inflater.inflate(R.layout.tablerow, tl, false);
TextView content = (TextView)row.findViewById(R.id.content);
content.setText("this is the content");
tl.addView(row);
This will allow you to set your layout, appearance, layout params in xml making it much easier to read and debug.
For the scrolling problem, you'll need to add your TableLayout to a ScrollView. Something like this in your xml:
<ScrollView>
<TableLayout android:id="#+id/arcitle_content_table" />
</ScrollView>
To wrap text in table rows:
By default, TableLayout rows fit the width of their content, no matter it goes over the screen bounds. To get the wider-than-screen text cells to wrap to multi-line, use android:shrinkColumns attribute on TableLayout.
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:shrinkColumns="*" />
android:shrinkColumns is zero-based index of the columns to shrink. It removes unnecessary extra space from a column and shrinks it :
android:shrinkColumns="*" shrinks all columns
android:shrinkColumns="0" shrinks first column
android:shrinkColumns="1,2" shrinks the second and third columns
android:stretchColumns does the opposite. It stretches a column to the maximum available width.
Both "shrink" and "stretch" consider all rows of the table to compute space.
To scroll down a TableLayout:
If your TableLayout is higher than the screen, move it in a ScrollView.

Categories

Resources