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.
Related
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));
I've seen non-scrollable ScrollView's a few times on stackoverflow.com, but none of the proposed solutions work for me. Ihope someone can help.
I have a ScrollView containing a TableLayout. The TableLayout is empty, but I programmatically insert rows and columns. I am guessing that I need to tell the ScrollView that the TableLayout has changed/updated, and ScrollView needs to recalculate if it needs to enable scrolling.
I tried to invalidate both the ScrollView and the TableLayout, I tried requestLayout(), but nothing seems to do anything. What am I missing?
Here is my layout XML:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:scrollbars="horizontal|vertical" >
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:id="#+id/maintable" >
</TableLayout>
</ScrollView>
And here is my Java code. Basicly, this code adds a row per player and 25 buttons next to each player. The buttons are there, but they are out of the screen boundaries.
// Get the TableLayout
TableLayout tl = (TableLayout) findViewById(R.id.maintable);
// Go through each item in the array
for (int player = 0; player < players.length; player++)
{
// Create a TableRow and give it an ID
TableRow tr = new TableRow(this);
tr.setId(100+player);
tr.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
// Create a TextView to add the player name
TextView labelTV = new TextView(this);
labelTV.setId(200+player);
labelTV.setText(players[player]);
//labelTV.setTextColor(Color.BLACK);
labelTV.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(labelTV);
// numShots = 25
for (int shot = 0; shot < numShots; shot++)
{
Button shotButton = new Button(this);
shotButton.setId((player * 100) + shot);
shotButton.setText(Integer.toString(shot));
tr.addView(shotButton);
}
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
// Add the TableRow to the TableLayout
//};
}
I think you're needing a horizontal scroll? ScrollView only does vertical scrolling, you'll need to have a look at HorizontalScrollView.
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.
I need to dynamically add some tablerows to a linearlayout in my app. I write this code:
LinearLayout tabella = (LinearLayout) findViewById(R.id.tabella_contatori);
for(int i =0; i<array_list.size(); i++){
TableRow row = new TableRow(getApplicationContext());
row.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
TextView data = new TextView(getApplicationContext());
data.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0.2f));
data.setTextAppearance(getApplicationContext(), android.R.attr.textAppearanceMedium);
data.setTextColor(Color.BLACK);
data.setBackgroundColor(Color.WHITE);
data.setPadding(2, 0, 0, 0);
data.setText("asd");
row.addView(data);
tabella.addView(row);
}
}
But when I open the app nothing appens. I already check if array_list.size is bigger than 0.
How can I do?
Thanks, Mattia
The problem is in your TextView Layout Params. The type should be TableRow.LayoutParams not LinearLayout.LayoutParams.
data.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
Take Table layout and try using this in your main.xml ...
<?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">
<TextView android:text="Some Text"/>
</TableRow>
</TableLayout>
in your Activity
this.setContentView(R.layout.main);
/* Find Tablelayout defined in main.xml */
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 TextView to be the row-content. */
TextView data = new TextView(getApplicationContext());
data.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 0.2f));
data.setTextAppearance(getApplicationContext(), android.R.attr.textAppearanceMedium);
data.setTextColor(Color.BLACK);
data.setBackgroundColor(Color.WHITE);
data.setPadding(2, 0, 0, 0);
data.setText("asd");
/* Add TextView to row. */
tr.addView(data);
/* Add row to TableLayout. */
tl.addView(tr,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
I recently started to play around with Android. One of the first things I tried to achieve was creating a dynamic TableLayout. Searching the web, I found some sample code at http://en.androidwiki.com/wiki/Dynamically_adding_rows_to_TableLayout which I copied into my activity.
So my Activity now looks like this:
public class FooActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.foo);
/* Find Tablelayout defined in main.xml */
TableLayout tl = (TableLayout)findViewById(R.id.myTableLayout);
for(int i= 0; i < 9; i++){
/* Create a new row to be added. */
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.setBackgroundColor(Color.MAGENTA);
/* 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));
}
}
}
my foo.xml in the layouts folder looks like this:
<?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"/>
</TableRow>
</TableLayout>
When I now start my app in the emulator, none of the dynamically added rows are visible. I just see the static button which has been defined in the foo.xml file. With an attached debugger, I've been able to see that my code gets executed and the rows are added to the tablelayout object. However, the added rows aren't rendered.
found the answer here:
Dynamically add TableRow to TableLayout
I've added a wrong import with Eclipse. Changing it to the right one, as described in the question above, fixed by problem