how to put a custom view in a table row in android? - android

I am creating table rows programmatically and i want to put a view that i created before in a row. Is there any way to do it?
for ( current = 0; current < rapor1.length; current++)
{
TableRow tr = new TableRow(this);
tr.setId(100+current);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView f1 = new TextView(this);
f1.setId(200+current);
f1.setText(rapor1[current]);
f1.setTextColor(Color.BLACK);
f1.setWidth(100);
tr.addView(f1);
DrawView dv = new DrawView(this);
tr.addView(dv);
it should be working but something is wrong.

I think it may helps. Just change this string:
tr.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
And of course you need to set LayoutParams to any view that you want to show on your screen

Related

dynamically added table is not displayed in android

there is something going wrong , such that firstly i created separately xml file for table row ,it did not work, then i created table row and text view programatically, again it is not displayed when i run , i can not get if what is wrong with my code
table = (TableLayout) findViewById(R.id.goodsTable);
for (int i = 0; i < attrName.length; i++) {
TableRow tr = new TableRow(getApplicationContext());
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
TextView atName = new TextView(getApplicationContext());
atName.setText(attrName[i]);
tr.addView(atName);
table.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
}
table.requestLayout();
Try This it may be help to you
for (int i = 0; i < attrName.length; i++) {
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
TextView atName = new TextView(this);
atName.setText(attrName[i]);
atName.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
tr.addView(atName);
table.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
}
You must apply LayoutParam for TextView and for create new Object for TableRow or TextView you should use Activity Context not ApplicationContext

Adding TableRows dynamically in Android doesn't work

I got a little problem with adding tablerows to a table layout in android.
Here's my code:
int z = 0;
for(String detail: details){
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
//odd / even
if(z%2 == 0)
tr.setBackgroundColor(Color.parseColor("#F5F5F5"));
//set detail text
TextView detailText = new TextView(this);
RelativeLayout.LayoutParams dtlp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
detailText.setTextSize(16.0f);
detailText.setTextColor(Color.parseColor("#333333"));
detailText.setPadding(20, 10, 20, 10);
detailText.setLayoutParams(dtlp);
detailText.setText(detail);
tr.addView(detailText);
detailsTable.addView(tr);
++z;
}
I cannot figure out where the problem is. The detail textview is being set but the tablerows won't show up.
a. Try changing dtlp's "width parameter" to "RelativeLayout.MATCH_PARENT"
OR
b. Before For loop block create two layout params like this.
TableLayout.LayoutParams tlps=new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,TableLayout.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams trps=new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT);
In loop Replace
detailText.setLayoutParams(dtlp);
WITH
detailText.setLayoutParams(trps);
And replace
detailsTable.addView(tr);
with
detailsTable.addView(tr,tlps);
hope this helps.

Multiple rows for table in android application

I am a starter in android application development. Kindly help me how to create multiple rows in a table and key performance to create a row. I am able to create 3 rows now. but i want to autogenerate the rows while entering the datas in table.
try this:
//Declare mytable in layout XML file.
TableLayout tl = (TableLayout) findViewById(R.id.mytable);
//loop through number of times u want to create row.
for (int current = 0; current < numofrowsrequired; current++)
{
// Create a TableRow and give it an ID
TableRow tr = new TableRow(this);
tr.setId(100+current);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//add required views with required parameters
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);
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}

how to apply textclick in tableview

In android, through xml parsing i have xml file values in tableview now which are stored in an array.
Now my question is i want to apply textclick on that values.
For example: if values are
Lotus
Lily
Rose
Jasmine
and i want to click on Rose
then how can i take particular array value from the table and apply intent on it???
the for loop that i have applied in parsing from where im getting array values:
for (int i = 0; i < data.getName().size(); i++) {
TableRow tr = new TableRow(this);
//tr.setId(100+i);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
// Create a TextView to house the name of the province
name[i]= new TextView(this);
name[i].setId(200+i);
name[i].setText(data.getName().get(i));
name[i].setTextColor(Color.WHITE);
name[i].setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(name[i]);
value[i] = new TextView(this);
value[i].setId(i);
value[i].setText(data.getValue().get(i));
value[i].setTextColor(Color.WHITE);
value[i].setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(value[i]);
t1.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
}
Well, if it's TableView, than it has some rows in it. Depending on implementation, you may wrap your textView into, for instance, linear layout and add click listener to it. For more information please be more specific regarding the issue.

How can I add extra TableRows to my TableLayout depending on cursor?

I have a database with different results in my database in my Android project. I want to fetch these results and show them in a tablelayout. In the tablelayout I want to add a row for each result, and write the column "date" on one textview, and the "result" on the second textview. Both textviews are on the same row. I fetch the data and store it in a cursor.
My question is: How do I use the cursor and add a row depending on how much data there is in my database?
Right now I try to add a row for each row in the cursor like this:
TableLayout tl = (TableLayout)findViewById(R.id.table_layout2);
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView mTvDate = new TextView(this);
TextView mTvResult = new TextView(this);
int x=0;
for(int i=0;i<c.getCount();i++){
mTvDate.setText(date[x]);
mTvResult.setText(""+nocorrect[x]+" av "+ noqs[x] +" ("+ proc[x]+ "%)");
mTvDate.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
mTvResult.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(mTvDate);
tr.addView(mTvResult);
tl.addView(tr,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
But I think I need to create a NEW tablerow, cause I get this error message:
02-22 09:22:17.543: ERROR/AndroidRuntime(333): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
But I don't want to remove it, I want to add more. Any ideas? Thanks!
Hi try this
i think you adding same row every time. so just declare it every loop iteration.
TableLayout tl = (TableLayout)findViewById(R.id.table_layout2);
int x=0;
for(int i=0;i<c.getCount();i++){
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView mTvDate = new TextView(this);
TextView mTvResult = new TextView(this);
mTvDate.setText(date[x]);
mTvResult.setText(""+nocorrect[x]+" av "+ noqs[x] +" ("+ proc[x]+ "%)");
mTvDate.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
mTvResult.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(mTvDate);
tr.addView(mTvResult);
tl.addView(tr,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}

Categories

Resources