Resizing Button in a TableLayout, Dynamically (Android) - 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.

Related

How to modify ImageButton layout

I have a TableRow where I add some items to be shown and everything goes alright. Nevertheless, when I want to add a layout-modified button it just does not appear to be painted. Here is the code:
ImageButton botonBorrar = new ImageButton(this);
botonBorrar.setImageResource(R.drawable.discard_light);
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.MATCH_PARENT);
buttonParams.setMargins(15, 10, 10, 10);
botonBorrar.setLayoutParams(buttonParams);
myRow.addView(botonBorrar);
What could be wrong here?. I have followed a lot of posts about this but nothing seems to correct my problem.
Thanks in advance.
I suppose you should use TableRow.LayoutParams instead of LinearLayout.LayoutParams.
The children of a TableRow do not need to specify the layout_width and layout_height attributes in the XML file. (http://developer.android.com/reference/android/widget/TableRow.html)
So you can add button to the view first and then get already generated layoutparams and modify them:
myRow.addView(botonBorrar);
TableRow.LayoutParams buttonParams = (TableRow.LayoutParams)bottonBorrar.getLayoutParams();
buttonParams.setMargins(15, 10, 10, 10);
//botonBorrar.setLayoutParams(buttonParams); - if you add button to the view, you do not have to set modified layout params explicitly.

Add dynamically buttons to layout in android

I was wondering how can I add dynamically buttons to layout from code. I don;t know how many buttons I will should add. This number will be random. I want to add them in this way:
Any ideas how can I do that dynamically in this way?
You can do it this way to add button:
Button myButton = new Button(this);
myButton.setText("Button");
LinearLayout ll = (LinearLayout)findViewById(R.id.buttonlayout);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ll.addView(myButton, lp);
Button myButton1 = new Button(this);
myButton1.setText("Button1");
ll.addView(myButton1, lp);
create one linearlayout with horizontal orientation and add two buttons on that.. when you have more than two buttons then add another linearlayout with horizontal for that buttons..

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.

Dynamically load customized table cells in table

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.

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