I'm using eclipse + android SDK.
I'm trying to create a table with data provided by sensors. I get the information correctly, by i'm having problems to make a table to show it in XML dinamically.
This is the sensorinfo.XML:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1"
android:id="#+id/SensorInfoTableLayout">
<TableRow>
<TextView
android:layout_column="1"
android:text="#string/sensor_name_title"
android:padding="3dip" />
<TextView
android:layout_column="2"
android:text="#string/sensor_type_title"
android:padding="3dip" />
<TextView
android:layout_column="3"
android:text="#string/sensor_value_title"
android:padding="3dip" />
<TextView
android:layout_column="4"
android:text="#string/sensor_unit_title"
android:padding="3dip" />
</TableRow>
<View
android:layout_height="4dip"
android:background="#FF909090" />
</TableLayout>
And this is the code of my activity:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getSensorInfo();
setContentView(R.layout.sensorinfo);
setInfoByView();
}
And
private void setInfoByView()
{
/* Find Tablelayout defined in xml */
TableLayout myTableLayout = (TableLayout)findViewById(R.id.SensorInfoTableLayout);
/* Create a new row to be added. */
TableRow myTableRow = new TableRow(this);
myTableRow.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
/* Create a Button to be the row-content. */
Button b = new Button(this);
b.setText("Dynamic Button");
b.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
myTableRow.setBackgroundColor(5);
/* Add Button to row. */
myTableRow.addView(b);
/* Add row to TableLayout. */
myTableLayout.addView(myTableRow,new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
}
THIS IS A TEST, i'm trying to include a button dinamically, if work, i will put the information i get from getSensorInfo();
But my Activity looks like this:
What could I be doing wrong?
All the info i found was THIS, but the answers didn't help me.
First of all create one Table Layout in your XML file like this :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#BFD6E8">
<ScrollView android:id="#+id/image_scroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableLayout android:id="#+id/image_table"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TableLayout>
</ScrollView>
</LinearLayout>
Then use this code for creating dynamic row inside the Table Layout :
TableLayout image_table=null;
image_table=(TableLayout)findViewById(R.id.image_table);
for(int i=0; i<10; i++){
TableRow tableRow=new TableRow(this);
tableRow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tableRow.setGravity(Gravity.CENTER_HORIZONTAL);
tableRow.setPadding(5, 5, 5, 5);
for(int j=0; j<1; j++){
Button bttn=new Button(this);
bttn.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
bttn.setText(Hello);
tableRow.addView(image, 200, 200);
}
image_table.addView(tableRow);
}
FINAL (test) SOLUTION:
private void setInfoByView2()
{
TableLayout myTableLayout = null;
TableRow myTableRow = new TableRow(this);
myTableLayout = (TableLayout)findViewById(R.id.SensorInfoTableLayout);
myTableRow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
//myTableRow.setGravity(Gravity.CENTER_HORIZONTAL);
myTableRow.setPadding(5, 5, 5, 5);
Button bttn = new Button(this);
bttn.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
bttn.setText("Hello");
myTableRow.addView(bttn, 200, 200);
myTableLayout.addView(myTableRow);
}
And the button is visible now.
Related
I am trying to add rows to a table after a click to a button (so after the generation of the activity).
My XML code is:
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="0,1"
android:id="#+id/result_table">
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
style="#style/MyHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Col1" />
<TextView
style="#style/MyHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Col2" />
</TableRow>
</TableLayout>
So when I load the activity I have my table with my only row. Then when I click on my button I do:
resultTable = (TableLayout)findViewById(R.id.result_table);
LayoutParams rowLayoutParams = new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
LayoutParams cellLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TableRow tableRow;
TextView cellValue;
for(Asdf a : as) {
tableRow = new TableRow(this);
tableRow.setLayoutParams(rowLayoutParams);
cellValue = new TextView(this);
cellValue.setText(a.getText());
cellValue.setLayoutParams(cellLayoutParams);
cellValue.setTextAppearance(this, R.style.MyText);
tableRow.addView(cellValue);
cellValue = new TextView(this);
cellValue.setText("asdf");
cellValue.setLayoutParams(cellLayoutParams);
cellValue.setTextAppearance(this, R.style.MyText);
tableRow.addView(cellValue);
resultTable.addView(tableRow);
}
resultTable.invalidate();
But it doesn't add anything, and I am sure about that the loop iterates. I don't get any warning/exception in the logcat.
Can you help me please ??
Try to change the LayoutParams for the added Views like this:
TableLayout.LayoutParams rowLayoutParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams cellLayoutParams = TableRow.new LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
I have these three lists with the same number of elements
List<String>competitoinsIDs = new LinkedList<String>();
List<String>marks = new LinkedList<String>();
List<String>numOfQuestions = new LinkedList<String>();
I want to put the first element of each list in a tablerow , then the second element of each list in another tablerow, would you help me please, this is the xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableLayout
android:id="#+id/tlMarksTable"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dip"
android:weightSum="2" >
<TextView
android:id="#+id/tvMarksCompetitionID"
android:layout_weight="1"
android:text="Competition"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/tvMarksMarks"
android:layout_weight="1"
android:text="Marks"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/tvMarksQuestionsNum"
android:layout_weight="1"
android:text="Questions"
android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>
</TableLayout>
</FrameLayout>
First change your XML file to:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableLayout
android:id="#+id/tlMarksTable"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</TableLayout>
</FrameLayout>
We'll add all the content dynamically.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
List<String>competitoinsIDs = new LinkedList<String>();
List<String>marks = new LinkedList<String>();
List<String>numOfQuestions = new LinkedList<String>();
//make sure that the lists contain data or else display will be blank screen
TableRow.LayoutParams params1=new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,1.0f);
TableRow.LayoutParams params2=new TableRow.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
TableLayout tbl=(TableLayout) findViewById(R.id.tlMarksTable);
for(int ctr=0;ctr<marks.size();ctr++)
{
//Creating new tablerows and textviews
TableRow row=new TableRow(this);
TextView txt1=new TextView(this);
TextView txt2=new TextView(this);
TextView txt3=new TextView(this);
//setting the text
txt1.setText(competitoinsIDs.get(ctr));
txt2.setText(marks.get(ctr));
txt3.setText(numOfQuestions.get(ctr));
txt1.setLayoutParams(params1);
txt2.setLayoutParams(params1);
txt3.setLayoutParams(params1);
//the textviews have to be added to the row created
row.addView(txt1);
row.addView(txt2);
row.addView(txt3);
row.setLayoutParams(params2);
tbl.addView(row);
}
}
please see the android code here and use it in your activities onCreate method
setContentView(R.layout.table_layout_xml); // use your layout xml file here
TableLayout tableLayout = (TableLayout)findViewById(R.id.tlMarksTable);
List<String>competitoinsIDs = new LinkedList<String>();
List<String>marks = new LinkedList<String>();
List<String>numOfQuestions = new LinkedList<String>();
// adding static values to test the layout. use your dynamic data here
competitoinsIDs.add("123");
competitoinsIDs.add("124");
competitoinsIDs.add("125");
marks.add("56");
marks.add("57");
marks.add("58");
numOfQuestions.add("10");
numOfQuestions.add("11");
numOfQuestions.add("12");
TableRow tableRows[] = new TableRow[competitoinsIDs.size()];
for (int i = 0; i < tableRows.length; i++) {
tableRows[i] = new TableRow(this);
tableRows[i].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tableRows[i].setWeightSum(2.0f);
tableRows[i].setPadding(5, 5, 5, 5);
TextView text = new TextView(this);
text.setLayoutParams(new android.widget.TableRow.LayoutParams(android.widget.TableRow.LayoutParams.WRAP_CONTENT,
android.widget.TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
text.setText(competitoinsIDs.get(i));
tableRows[i].addView(text);
text = new TextView(this);
text.setLayoutParams(new android.widget.TableRow.LayoutParams(android.widget.TableRow.LayoutParams.WRAP_CONTENT,
android.widget.TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
text.setText(marks.get(i));
tableRows[i].addView(text);
text = new TextView(this);
text.setLayoutParams(new android.widget.TableRow.LayoutParams(android.widget.TableRow.LayoutParams.WRAP_CONTENT,
android.widget.TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
text.setText(numOfQuestions.get(i));
tableRows[i].addView(text);
tableLayout.addView(tableRows[i]);
}
I'm trying to build a part of a Android View Programmatically, but unfortunately I'm having some problems with the RelativeLayout. it makes my Elements overlay on eachother
This is my Code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.insertnew_layout);
LinearLayout ll = (LinearLayout) findViewById(R.id.container);
ll.setOrientation(LinearLayout.VERTICAL);
TableLayout tl = new TableLayout(this);
tl.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
// fill content
for (int i = 0; i < 3; i++) {
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
RelativeLayout rl = new RelativeLayout(this);
rl.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
TextView score = new TextView(this);
score.setText(""+i);
RelativeLayout.LayoutParams lpScore = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lpScore.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
score.setLayoutParams(lpScore);
TextView description = new TextView(this);
description.setText("this is my description");
RelativeLayout.LayoutParams lpDescription = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lpDescription.addRule(RelativeLayout.RIGHT_OF, score.getId());
description.setLayoutParams(lpDescription);
CheckBox checkbox = new CheckBox(this);
RelativeLayout.LayoutParams lpCheckbox = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lpCheckbox.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
checkbox.setLayoutParams(lpCheckbox);
rl.addView(score);
rl.addView(description);
rl.addView(checkbox);
tl.addView(rl);
}
ll.addView(tl);
This is what it looks like:
As you can see, the "description" is on top of of the "score".
Here is the same code in xml:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/tableLayoutTextEntry" >
<TableRow
android:id="#+id/tableRowTextEntry"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="score" />
<TextView
android:id="#+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/score"
android:text="description" />
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="select" />
</RelativeLayout>
</TableRow>
</TableLayout>
And here is what it looks like in xml:
As you can see, in xml, the toRightOf-command works as expected - the description is to the right of the score
How is that possible? Shouldnt the line
lpDescription.addRule(RelativeLayout.RIGHT_OF, score.getId());
do the same thing?
At the moment you are calling it, score.getId() will return -1 (invalid), because it has not been added yet to the screen with the whole layout processed.
You will have to set the id manually with setId() before calling getId() and it should all work fine.
I'm trying to make my tablelayout which is nested in a relativeview scrollable.
Tried a couple of tutorials but nothing worked. Here is the xml and my java code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#color/white" android:padding="10px"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/textView1"
android:paddingBottom="10dp" android:text="Mijn Rooster"
android:textSize="20sp" android:layout_alignParentTop="true"
android:layout_alignLeft="#+id/tablelayout"></TextView>
<ScrollView android:id="#+id/ScrollView01"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<TableLayout android:id="#+id/tablelayout"
android:layout_width=
"fill_parent" android:layout_height="wrap_content"
android:stretchColumns="0" android:layout_below="#+id/textView1"
android:layout_above="#+id/btnsearch">
</TableLayout>
</ScrollView>
<Button android:id="#+id/btnsearch" android:layout_width="wrap_content"
android:text="Zoek op Datum" android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"></Button>
</RelativeLayout>
And here is the JAVA code:
TableLayout tl = (TableLayout) findViewById(R.id.tablelayout);
if (date_selected == null) {
for (int i = 0; i < roostermap.size(); i++) {
TableRow trdaydatetime = new TableRow(this);
TableRow trinfo = new TableRow(this);
trdaydatetime.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
TableRow.LayoutParams rowSpanLayout = new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
rowSpanLayout.span = 3;
TextView txtDay = new TextView(this);
TextView txtTime = new TextView(this);
TextView txtResult = new TextView(this);
// Set Text
txtDay.setText(roostermap.get(i).getDay(
roostermap.get(i).getRoster_date().getDay(),
roostermap.get(i).getRoster_date())
+ " " + df.format(roostermap.get(i).getRoster_date()));
txtTime.setText(dft.format(roostermap.get(i).getRoster_start())
+ " - " + dft.format(roostermap.get(i).getRoster_end()));
txtResult.setText(roostermap.get(i).getWorkplace_name());
// Day&Date Layout
txtDay.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
txtDay.setPadding(5, 5, 0, 0);
txtDay.setTextColor(Color.BLACK);
// Text Time layout
txtTime.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
txtTime.setTextColor(Color.BLACK);
txtTime.setPadding(0, 5, 10, 0);
txtTime.setGravity(Gravity.RIGHT);
// Text Result layout
txtResult.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
txtResult.setPadding(5, 0, 0, 5);
trdaydatetime.addView(txtDay);
trdaydatetime.setBackgroundResource(R.drawable.trup);
trdaydatetime.addView(txtTime);
trinfo.addView(txtResult, rowSpanLayout);
trinfo.setBackgroundResource(R.drawable.trdown);
tl.addView(trdaydatetime, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tl.addView(trinfo, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}
In order for the 'scrolling' to work you need the content of the ScrollView to be larger than the ScrollView itself. Since you have set the android:layout_height value to wrap_content, the height of the ScrollView matches the height of its content - which means no scrolling.
Fix the android:layout_height value of the ScrollView by either setting it to fill_parent (possibly with android:layout_weight) or a pixel value.
Eliminate the ScrollView. You cannot combine two Views which are both scrollable, especially if the scroll in the same direction.
A TableView itself handles scrolling if the content is larger then the height of the TableView.
I'm trying to add table rows programmatically following the code here
/* Find Tablelayout defined in main.xml */
TableLayout tl = (TableLayout) findViewById(R.id.SaleOrderLines);
/* Create a new row to be added. */
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
/* Create a Button to be the row-content. */
Button b = new Button(this);
b.setText("Dynamic Button");
b.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
/* Add Button to row. */
tr.addView(b);
/* Add row to TableLayout. */
//tr.setBackgroundResource(R.drawable.sf_gradient_03);
tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
But but nothing is drawn to the screen. (same as one mentioned here)
When i added tr.setBackgroundResource(R.drawable.sf_gradient_03);, the row with the background image is drawn but not the button
Here is my Layout
<!-- Sale Order Lines START -->
<LinearLayout android:id="#+id/SaleOrderLinesArea" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:padding="10dip" android:background="#drawable/sf_gradient_11" >
<LinearLayout android:id="#+id/subHeaderArea" android:padding="10dip"
android:layout_width="fill_parent" android:layout_height="wrap_content"
>
<TextView android:id="#+id/screenTitle"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textColor="#color/title_sub" android:textStyle="bold"
android:text="Order Lines" />
</LinearLayout>
<TableLayout android:id="#+id/SaleOrderLines"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:padding="10dip" android:stretchColumns="*">
<TableRow
android:background="#drawable/sf_gradient_13" android:padding="10dip"
>
<TextView android:id="#+id/order_ref_label"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:textColor="#color/fg_prime" android:text="bLA bLA" />
<TextView android:id="#+id/product_label"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textStyle="bold" android:textColor="#color/fg_title"
android:text="#string/product" />
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textStyle="bold" android:textColor="#color/fg_title"
android:text="#string/product_quantity" />
</TableRow>
<TableRow android:background="#drawable/sf_gradient_03"
android:paddingLeft="10dip" android:paddingRight="10dip"
>
<TextView android:id="#+id/order_ref_label"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:textColor="#color/fg_prime" android:text="Fooo" />
<Spinner android:id="#+id/product_spinner"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:prompt="#string/customer_prompt">
</Spinner>
<EditText android:id="#+id/product_uom_qty"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:singleLine="true" android:fadingEdge="horizontal" />
</TableRow>
</TableLayout>
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content"
android:paddingTop="5dip" android:paddingBottom="5dip">
<Button android:id="#+id/add_button" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Add Lines" />
</LinearLayout>
</LinearLayout>
<!-- Sale Order Lines END -->
Got it, every LayoutParams should be of android.widget.TableRow.LayoutParams except one that supplied to tl.addView(...)
/* Find Tablelayout defined in main.xml */
TableLayout tl = (TableLayout) findViewById(R.id.SaleOrderLines);
/* Create a new row to be added. */
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
/* Create a Button to be the row-content. */
Button b = new Button(this);
b.setText("Dynamic Button");
b.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
/* Add Button to row. */
tr.addView(b);
/* Add row to TableLayout. */
//tr.setBackgroundResource(R.drawable.sf_gradient_03);
tl.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
Please do note that, may be your code is perfect but the classes you are using are not proper. You may have used import android.view.ViewGroup.LayoutParams , where as the correct class is available from following import statement :
import android.widget.TableRow.LayoutParams
In my case, I changed the width from TableRow.LayoutParams.WRAP_CONTENT to TableRow.LayoutParams.MATCH_PARENT, then it works.
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));