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);
}
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);
}
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 have an array of length n, I now need to create n number of LinearLayouts and add different stuffs on each of them.
How can it be done dynamically?
LinearLayout lLayout = new LinearLayout(context);
parentWidget.addView(lLayout);
The easiest way is to create a layout in xml and inflate it using
LayoutInflater.from(context).inflate(R.layout.my_linear_layout);
You may also want to setId() your added views so you can access them easily later on.
I solved it using RelativeLayout which I found a little easier to work with. Yes of-course like the guys pointed out above I used setId(). Here is the code I implemented:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ScrollView sv = new ScrollView(this);
//Parent RelativeLayout
parentLayout = new RelativeLayout(this);
parentLayout.setBackgroundColor(Color.WHITE);
params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
parentLayout.setLayoutParams(params);
sv.addView(parentLayout);
final String[] comList = getCommunication();
int listLength=0;
try{
listLength= comList.length/3;
}catch(Exception e){System.out.println(e);System.exit(0);}
childLayout= new RelativeLayout[listLength] ;
TextView[] tvName = new TextView[listLength];
TextView[] tvDate =new TextView[listLength];
TextView[] tvMsg =new TextView[listLength];
for(int i =0;i<listLength;i++){
try{
childLayout[i] = new RelativeLayout(this);
childLayout[i].setPadding(5, 5, 5, 5);
params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 75);
if(i==0){params.addRule(RelativeLayout.BELOW);}
else{params.addRule(RelativeLayout.BELOW,i);}
childLayout[i].setId(i+1);
childLayout[i].setClickable(true);
childLayout[i].setLayoutParams(params);
childLayout[i].setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
//Create the intent
Intent i = new Intent("ACTIIVTY");
startActivity(i);
}
});
tvName[i] = new TextView(this);
params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
tvName[i].setLayoutParams(params);
childLayout[i].addView(tvName[i]);
if(comList[i*3].length()>24){
String name = comList[i*3].substring(0,24)+"...";
tvName[i].setText(name);
}else{
tvName[i].setText(comList[i*3]);
}
tvName[i].setId(listLength+1+i);
tvName[i].setTextSize(12);
tvName[i].setTextColor(Color.BLACK);
tvDate[i] = new TextView(this);
params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
tvDate[i].setLayoutParams(params);
childLayout[i].addView(tvDate[i]);
tvDate[i].setTextSize(11);
tvDate[i].setTextColor(Color.BLUE);
tvDate[i].setText(comList[i*3+1]);
tvMsg[i] = new TextView(this);
params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, listLength+1+i);
tvMsg[i].setLayoutParams(params);
childLayout[i].addView(tvMsg[i]);
tvMsg[i].setTextSize(11);
tvMsg[i].setTextColor(Color.GRAY);
if(comList[i*3+2].length()>96){
String msg = comList[i*3+2].substring(0,96)+"...";
tvMsg[i].setText(msg);
}else{
tvMsg[i].setText(comList[i*3+2]);
}
parentLayout.addView(childLayout[i]);
}catch(Exception e){System.out.println("Errrorrrrr");}
}
setContentView(sv);
}
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);
}