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...
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 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);
}
In this code I'm building a method that adds the same image a number of times to a Relative Layout at runtime. However, the images are not showing up when debugging. The textview that was added is acting normally. Can someone explain how to make the images visible? Thanks in advance.
My Code:
public class TestActivity extends Activity {
ArrayClass arraysObject1 = new ArrayClass();
ArrayList<ImageView> mImages = new ArrayList<ImageView>();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
layout.setLayoutParams(params);
layout.setBackgroundColor(Color.parseColor("#FFFFFF"));
//textview is working
TextView testText = new TextView(this);
testText.setText("Alle aangeboden vacatures vindt u hieronder terug.");
testText.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
layout.addView(testText);
RelativeLayout.LayoutParams imgParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
imgParams.addRule(RelativeLayout.CENTER_IN_PARENT);
for(int i=0; i<arraysObject1.array1.size(); i++)
{
mImages.add(new ImageView(this));
mImages.get(i).setVisibility(View.VISIBLE);
mImages.get(i).setBackgroundResource(R.drawable.work);
mImages.get(i).setLayoutParams(imgParams);
layout.addView(mImages.get(i));
}
setContentView(layout);
}
}
Your cycle for(int i=0; i < arraysObject1.array1.size(); i++) seemed not to be executed.
arraysObject1.array1 is empty.
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"
/>
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);
}