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));
Related
I have the following layout:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableLayout
android:id="#+id/master_table"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1">
</TableLayout>
</LinearLayout>
In code, I add some buttons to the table layout which will populate added buttons by 4 per row:
To add buttons:
TableRow row = createTableRow();
row.addView(createToggleButton(R.id.button1, getResources().getText(R.string.button1)));
row.addView(createToggleButton(R.id.button2, getResources().getText(R.string.button2)));
row.addView(createToggleButton(R.id.button3, getResources().getText(R.string.button3)));
row.addView(createToggleButton(R.id.button4, getResources().getText(R.string.button4)));
TableLayout parent = (TableLayout)findViewById(R.id.master_table);
parent.addView(firstRow);
Original version of createToggleButton() method:
private ToggleButton createToggleButton(int id, CharSequence text) {
ToggleButton button = new ToggleButton(this);
button.setId(id);
button.setTextOff(text);
button.setTextOn(text);
button.setChecked(false);
button.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.button, this.getTheme()));
button.setTextColor(getResources().getColorStateList(R.color.button_color));
return button;
}
This work, my buttons are added to the view. But when I try to apply LayoutParams as below:
private ToggleButton createToggleButton(int id, CharSequence text) {
ToggleButton button = new ToggleButton(this);
// This line cause the button disappeared !?
button.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f));
button.setId(id);
button.setTextOff(text);
button.setTextOn(text);
button.setChecked(false);
button.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.button, this.getTheme()));
button.setTextColor(getResources().getColorStateList(R.color.button_color));
return button;
}
My buttons no longer appear on the view even though there are no error occurred. What happens here?
The error should be caused by the width of TableRow and its ToggleButton children are interdependent.
set LayoutParam for TableRow
row.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
Also change the Layout of ToggleButton to this
button.setLayoutParams(new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
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));
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));
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.
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