HI am doing table layout programatically for that when i add table row to the layout application shows force close can any body help me and following is my code
TableLayout tl = (TableLayout) findViewById(R.id.maintable);
TableRow tr = new TableRow(this);
TextView labelTV = new TextView(this);
TextView valueTV = new TextView(this);
// Go through each item in the array
for (int current = 0; current < numProvinces; current++)
{
// Create a TableRow and give it an ID
tr.setId(100+current);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
// Create a TextView to house the name of the province
labelTV.setId(200+current);
labelTV.setText(provinces[current]);
labelTV.setTextColor(Color.WHITE);
labelTV.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(labelTV);
// Create a TextView to house the value of the after-tax income
valueTV.setId(current);
valueTV.setText("$0");
valueTV.setTextColor(Color.WHITE);
valueTV.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(valueTV);
// Add the TableRow to the TableLayout
tl.addView(tr,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
setContentView(tl);
}
TableLayout tl = (TableLayout) findViewById(R.id.maintable);
TableRow[] tr;
TextView[] labelTV;
TextView[] valueTV;
tr = new TableRow[numProvinces];
labelTV = new TextView[numProvinces];
valueTV= new TextView[numProvinces];
tl.removeAllViews();
tl.refreshdrawablestate();
for (int current = 0; current < numProvinces; current++)
{
tr[current] = new TableRow(this);
labelTV[current] = new TextView(this);
labelTV[current] = new TextView(this);
// Create a TableRow and give it an ID
tr.setId(100+current);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
// Create a TextView to house the name of the province
labelTV.setId(200+current);
labelTV.setText(provinces[current]);
labelTV.setTextColor(Color.WHITE);
labelTV.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(labelTV);
// Create a TextView to house the value of the after-tax income
valueTV.setId(current);
valueTV.setText("$0");
valueTV.setTextColor(Color.WHITE);
valueTV.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(valueTV);
// Add the TableRow to the TableLayout
tl.addView(tr,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
setContentView(tl);
TableLayout tl = (TableLayout) findViewById(R.id.maintable);
for (int current = 0; current < numProvinces; current++)
{
TableRow tr = new TableRow(this);
TextView labelTV = new TextView(this);
TextView valueTV = new TextView(this);
/// Do your stuff
tr.addView(labelTV );
tr.addView(valueTV );
tbl.addView(tr);
}
Try this answer
The view already exist may be the Exception you are getting so,
TableRow tr = new TableRow(this);
add the above line in side for loop..
This is happening because you are trying to add same TableRow again and again..
Updated:
try this
tl.addView(tr);
instead of
tl.addView(tr,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
I tested your code with a change in xml file and it work. I post my code so that you can compare with your own one.
I copy your code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//I add some data for test here:
int numProvinces = 5;
String[] provinces =new String[]{"1","2","3","4","5"};
//below is the same with your code:
TableLayout tl = (TableLayout) findViewById(R.id.maintable);
// Go through each item in the array
for (int current = 0; current < numProvinces; current++)
{
TableRow tr = new TableRow(this);
TextView labelTV = new TextView(this);
TextView valueTV = new TextView(this);
// Create a TableRow and give it an ID
tr.setId(100+current);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
// Create a TextView to house the name of the province
labelTV.setId(200+current);
labelTV.setText(provinces[current]);
labelTV.setTextColor(Color.WHITE);
labelTV.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(labelTV);
// Create a TextView to house the value of the after-tax income
valueTV.setId(current);
valueTV.setText("$0");
valueTV.setTextColor(Color.WHITE);
valueTV.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(valueTV);
// Add the TableRow to the TableLayout
tl.addView(tr,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
setContentView(tl);
}
}
and this is my main layout (main.xml)
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/maintable"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
Related
I am trying to dynamically generate my activity layout but for some reason I can't get my TableLayout or my TableRow to match the parent width. I am using a relative layout, then I create my TableLayout and fill it with rows before adding my TableLayout to my RelativeLayout.
Here is my onCreate:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
context = this;
RelativeLayout mainLayout = new RelativeLayout(context);
RelativeLayout.LayoutParams relativeLayout = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT
);
mainLayout.setLayoutParams(relativeLayout);
//Create the signature views
//createPage(mainLayout);
setupPage(mainLayout);
setContentView(mainLayout);
}
Here is my setupPage() code:
private void setupPage(RelativeLayout mainLayout)
{
TableLayout mainTable = new TableLayout(context);
RelativeLayout.LayoutParams tableLayoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
mainLayout.setLayoutParams(tableLayoutParams);
mainTable.setBackgroundResource(R.drawable.rectangle);
//JOB DESCRIPTION title
mainTable.addView(getRowTitle("JOB DESCRIPTION"));
//---- END ----
mainLayout.addView(mainTable);
}
private TableRow getRowTitle(String text)
{
TableRow row = new TableRow(context);
TableLayout.LayoutParams rowLayout = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.MATCH_PARENT
);
row.setLayoutParams(rowLayout);
row.setBackgroundResource(R.drawable.rectangle);//so i can see the outline of the row
/*TextView title = new TextView(context);
TableRow.LayoutParams editLayout = new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT
);
editLayout.setMargins(15,0,0,0);
title.setLayoutParams(editLayout);
title.setText(text);
row.addView(title);*/
return row;
}
You're not setting the LayoutParams on the proper view(which most likely ends up with the default LayoutParams). In the setupPage() method you do:
mainLayout.setLayoutParams(tableLayoutParams);
instead of setting it on the TableLayout:
mainTable.setLayoutParams(tableLayoutParams);
try this -
private void setupPage(RelativeLayout mainLayout)
{
TableLayout mainTable = new TableLayout(context);
RelativeLayout.LayoutParams tableLayoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
mainTable.setLayoutParams(tableLayoutParams);
mainTable.setBackgroundResource(R.drawable.rectangle);
//JOB DESCRIPTION title
mainTable.addView(getRowTitle("JOB DESCRIPTION"));
//---- END ----
mainLayout.addView(mainTable);
}
I have code:
LinearLayout primaryFieldsView = (LinearLayout) findViewById(R.id.mainLayout);
for(int j=0; j<5; j++){
TextView text = new TextView(this);
text.setText("The Value of i is :"); // <-- does it really compile without the + sign?
text.setTextSize(12);
text.setGravity(Gravity.LEFT);
text.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
primaryFieldsView.addView(text);
}
I would like to that this code was called from another class, for example:
In main class:
new PrimaryFieldsView().setPrimaryFields();
Other class:
public class PrimaryFieldsView extends Activity{
public void setPrimaryFields(){
LinearLayout primaryFieldsView = (LinearLayout) findViewById(R.id.mainLayout);
for(int j=0; j<5; j++){
TextView text = new TextView(this);
text.setText("The Value of i is :"); // <-- does it really compile without the + sign?
text.setTextSize(12);
text.setGravity(Gravity.LEFT);
text.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
primaryFieldsView.addView(text);
}
}
When i put this code in Main class that work:
LinearLayout primaryFieldsView = (LinearLayout) findViewById(R.id.mainLayout);
for(int j=0; j<5; j++){
TextView text = new TextView(this);
text.setText("The Value of i is :"); // <-- does it really compile without the + sign?
text.setTextSize(12);
text.setGravity(Gravity.LEFT);
text.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
primaryFieldsView.addView(text);
}
At this moment i have error
10-27 21:32:58.389: E/AndroidRuntime(2907): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.passreader/com.example.passreader.views.CouponView}: java.lang.NullPointerException
public void setPrimaryFields(Activity activity)
{
LinearLayout primaryFieldsView = (LinearLayout) activity.findViewById(R.id.mainLayout);
for(int j=0; j<5; j++){
TextView text = new TextView(this);
text.setText("The Value of i is :"); // <-- does it really compile without the + sign?
text.setTextSize(12);
text.setGravity(Gravity.LEFT);
text.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
primaryFieldsView.addView(text);
}
}
in main:
setPrimaryFields(this);
PrimaryFieldsView class does not have to extend anything though.
I am trying to center a textview inside a tablerow, that is itself inside a tablelayout, but I guess I am missing something, because the TextView doesn't get centered :s
Here is my method:
private void insertClock() {
TableLayout table = new TableLayout(this);
table.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
table.setStretchAllColumns(true);
TableRow tbRow = new TableRow(this);
TableLayout.LayoutParams layoutRow = new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
clock = new TextView(this);
TableRow.LayoutParams layoutHistory = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
clock.setLayoutParams(layoutHistory);
clock.setText(calculateClockText());
tbRow.setLayoutParams(layoutRow);
table.addView(tbRow);
clock.setGravity(Gravity.CENTER_HORIZONTAL);
tbRow.addView(clock);
}
Thanks in advance ;)
Finnaly made it working !
Here is the solution:
private void insertClock(TableLayout table, String text) {
TableRow tbRow = new TableRow(this);
TableLayout.LayoutParams layoutRow = new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
tbRow.setLayoutParams(layoutRow);
clock = new TextView(this);
TableRow.LayoutParams layoutHistory = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
clock.setLayoutParams(layoutHistory);
clock.setText(text);
clock.setGravity(Gravity.CENTER);
tbRow.setGravity(Gravity.CENTER);
tbRow.addView(clock);
table.addView(tbRow);
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout ll=(LinearLayout)findViewById(R.id.linearlayout);
for(int k=0;k<10;k++)
{ TableRow tr=new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tr.setId(k);
for(int s=0;s<10;s++)
{
EditText edt=new EditText(this);
Log.i("SS","setting layout params for textview "+s+k);
edt.setText("abc "+s+k+" ");
edt.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
ViewGroup.LayoutParams p=edt.getLayoutParams();
Log.i("SS","edt params "+p.height +" "+p.width);
tr.addView(edt);
}
ll.addView(tr);
}
}
}
In the above code if i remove the line edt.setLayoutParams... the edit Texts are showing.
But if i set the their params they are not showing.
Any idea what could be the reason?
Try out This...
TableRow tr = new TableRow(this);
TableLayout.LayoutParams tableRowParams=
new TableLayout.LayoutParams
(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
tableRowParams.setMargins(10, 10, 10, 10);
tr.setLayoutParams(tableRowParams);
Hope this will help u out...
I am creating TableRows dynamically. And there are two types of content for these TableRows.
Some have 4 Views.
And some have 2 Views.
The problem is that TableRows with two, try to occupy the same space as the layout of which have four.
This is happening:
Img ThisTextViewHasThisSize
Img TV0 __________________TV1 TV2
...
TableLayout tl = (TableLayout) findViewById(R.id.myTableLayout);
for (int i = 0; i < array.length; i++) {
TableRow tr = new TableRow(this);
ImageButton button = new ImageButton(this);
tr.addView(button);
TextView tv0 = new TextView(this);
tv0.setText(array[i].something0());
if (another type of TableRow) {
TextView tv1 = new TextView(this);
TextView tv2 = new TextView(this);
tv1.setText(array[i].something1());
tv2.setText(array[i].something2());
tr.addView(tv1);
tr.addView(tv2);
}
tr.addView(tv0);
tl.addView(tr);
}
Someone can tell me how to let the TableRows layouts totally independent from each other?
You can set the span of a view within the table by using TableRow.LayoutParams. Eg:
TableRow tr = new TableRow(this);
//all rows have this button
ImageButton button = new ImageButton(this);
tr.addView(button);
TextView tv0 = new TextView(this);
tv0.setText("hey");
if (some condition == true) {
TextView tv1 = new TextView(this);
TextView tv2 = new TextView(this);
tv1.setText("heay 2");
tv2.setText("hey 3");
tr.addView(tv0);
tr.addView(tv1);
tr.addView(tv2);
}else {
TableRow.LayoutParams trlp = new TableRow.LayoutParams();
trlp.span = 3;
tv.setLayoutParams(trlp);
tr.addView(tv0);
}