Row cannot add dynamically using code to Table Layout in Android - android

I just want to add new rows at run time, and I tried using following codes..
java file ::>
TableLayout caseTable = (TableLayout) findViewById(R.id.caseTable);
TableRow caseRow = new TableRow(this);
caseRow.setOrientation(TableRow.VERTICAL);
EditText name = new EditText(this);
name.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
name.setText("Name __");
caseRow.addView(name);
caseTable.addView(caseRow,new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
xml file ::>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/caseTable"
android:stretchColumns="0,1,2,3">
<TableRow>
<TextView android:text="Case" />
<TextView android:text="Details" />
</TableRow>
</TableLayout>
I want to add new rows below the existing one. I get the code from here at stackOverFlow. But it didn't work. Thank you.

Just Simply Use The below code to add TableRow Programatically, perviously it is not working because you are trying to add TableLayout parameter to your EditText.
TableLayout caseTable = (TableLayout) findViewById(R.id.caseTable);
TableRow caseRow = new TableRow(this);
caseRow.setOrientation(TableRow.VERTICAL);
EditText name = new EditText(this);
//name.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
name.setText("Name __");
caseRow.addView(name);
caseTable.addView(caseRow,new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));

Related

If a ScrollView is the parent, it doesn't show a TableLayout into a RelativeLayout(Android)

I have this xml file:
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout android:id="#+id/conteiner_2"
android:layout_width="500px"
android:layout_height="500px"
android:background="#drawable/gradient_2">
</RelativeLayout>
</HorizontalScrollView>
</ScrollView>
And I am trying to create a TableLayout which if I delete the ScrollViews it appears, but when I let it like before the TableLayout isn't show.
This is my code:
TableLayout table = new TableLayout(this);
RelativeLayout.LayoutParams paramsTable = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
TableLayout.LayoutParams paramsRow = new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,0,1.0f);
TableRow.LayoutParams paramsCol = new TableRow.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT,1.0f);
for(int row = 0 ; row < rows ; row++){
TableRow row = new TableRow(this);
row.setLayoutParams(paramsRow);
for(int col = 0 ; col < column ; col++){
TextView text = new TextView(this);
text.setLayoutParams(paramsCol);
text.setText(row + "-" + col);
row.addView(text);
}
table.addView(row);
}
tabla.setLayoutParams(paramsTable);
conteiner.addView(table);
Sorry for my poor English, I hope I made myself understood.
<<< EDIT >>>
I have to put another layout inside the RelativeLayout, and it works.
I have to put another layout inside the RelativeLayout, and it works.

Android: 2 buttons in TableRow

I'm pretty new to android programming.
I want to add two buttons to a tableRow programmatically.
Here is the code wich should do it, and the XML of the table row:
Code:
public void buttons() {
Button button1 = new Button(this);
button1.setText("Fertig");
Button button2 = new Button(this);
button2.setText("Zurück");
tableRow.addView(button1);
tableRow.addView(button2);
}
XML:
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="2dp" >
</TableRow>
But if I run the code, it looks like this:
If I just add button1 (Remove tableRow.addView(button2); from my code), the button is at the right position, at the verry bottom left of the screen...
Can anybody help me, so both buttons will be displayed right?
Thank you! :)
Change your XML from TableRow to TableLayout and add each button into a single TableRow then Add your tableRow into your main TableLayout as following:
<TableLayout
android:id="#+id/table1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="2dp" >
</TableLayout>
in your Java class
TableLayout tl = (TableLayout) findViewById(R.id.table1);
/* Create a new row to be added. */
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_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.MATCH_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.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));

TableRow is being added but doesn't display

I have a small app that periodically needs a table row added to the tablelayout. Per the getChildCount() method the row is being added but it doesn't display. Any suggestions would be greatly appreciated
TableLayout tbl = (TableLayout)findViewById(R.id.mytbl);
TableRow row = new TableRow(this);
LinearLayout ll = new LinearLayout(this);
TextView txtView = new TextView(this);
row.setId(1234);
row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));
row.setGravity(Gravity.CENTER);
row.setBackgroundResource(android.R.color.darker_gray);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT,1.0f);
ll.setId(1589);
ll.setLayoutParams(lp);
ll.setOrientation(LinearLayout.VERTICAL);
ll.setBackgroundResource(R.color.header_background);
ll.setGravity(Gravity.CENTER);
String msg = "Sample Text";
txtView.setId(657);
txtView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
txtView.setTextColor(R.color.a_color);
txtView.setBackgroundResource(R.drawable.border_a);
txtView.setGravity(Gravity.TOP | Gravity.LEFT);
txtView.setText(msg);
ll.addView(txtView);
row.addView(ll);
tbl.addView(row,new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));//maybe add this with layout params tl.addView(tr,new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
i've tried tbl.RequestLayout() and tbl.Invalidate() but no help. The table is wrapped in a scrollview which is my best guess as to where the problem is. I've been tinkering with this for several hours so a nudge in the right direction sure would be helpful
<ScrollView
android:id="#+id/scrVw_lst"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TableLayout
android:id="#+id/mytbl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="*">
Have you run the code for adding a row in your TableLayout? I've simulated adding a row on a click of a Button and I get a awkward Divide by 0 exception.
Anyway, change the LayoutParams of the enclosing Linearlayout to TableRow.LayoutParams(its parent is the TableRow):
TableRow.LayoutParams lp = new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1.0f);
ll.setId(1589);
ll.setLayoutParams(lp)
Also, if this doesn't solve the problem, try to add:
android:fillViewport="true"
to the ScrollView tag in the xml layout.

Android: Lining up Views in TableRow programatically

An activity in my application (my first attempt at one) generates several table rows each containing a couple of EditTexts and a Button. But, the button always appears to be misaligned, resulting in this problem - the delete buttons are shifted slightly downwards.
The relevant part of the xml layout is as follows:
<ScrollView
android:id="#+id/countEditList"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableLayout
android:id="#+id/countEditLayout"
android:stretchColumns="*"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- insert the new rows here as they are created -->
</TableLayout>
</ScrollView>
The code which creates each row from a database cursor does the following:
TableRow row = new TableRow(this);
EditText title = new EditText(this);
title.setHorizontallyScrolling(true);
title.setGravity(Gravity.CENTER);
title.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
EditText counting = new EditText(this);
counting.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
counting.setGravity(Gravity.CENTER);
Button deleteCount = new Button(this);
deleteCount.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
deleteCount.setText(R.string.deleteCount);
deleteCount.setOnClickListener(this);
deleteCount.setGravity(Gravity.CENTER);
row.addView(title);
row.addView(counting);
row.addView(deleteCount);
layout.addView(row,new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); // Earlier: layout = (ViewGroup) findViewById(R.id.countEditLayout);
I suspect that I should somehow be setting the layout_gravity of the EditTexts and Button but I can't work out how to do that - would anyone be able to offer any suggestions?
You could adjust Edit Texts positioning using some padding:
title.setPadding(3, 3, 3, 3);
counting.setPadding(3, 3, 3, 3);

classcast exception while adding rows to table layout

problem solved: i should have cleaned the project. thanks for the answers
hey guys i'm trying to add rows to table layout durign execution but it gives an error
12-02 20:29:47.588: ERROR/AndroidRuntime(569): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.project.andr/com.project.andr.MainActivity}: java.lang.ClassCastException: android.widget.ScrollView
here is my xml file
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myTableLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button android:text="Static Button" android:id="#+id/myButton"/>
</TableRow>
</TableLayout>
and here is my java code
setContentView(R.layout.main);
TableLayout tl = (TableLayout)findViewById(R.id.myTableLayout);
/* Create a new row to be added. */
TableRow tr = new TableRow(this);
tr.setLayoutParams(new 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. */
tl.addView(tr,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
setContentView(tl);
It looks like the class cast exception is with your ScrollView, not your TableLayout. Give your whole stack trace or check to make sure you're using your ScrollView properly.
cleaning the project did the work for me.

Categories

Resources