Centering TableLayout rows using Java Code - android

I would to create a TableLayout and center its content.
I write this piece of code:
TableLayout tl = new TableLayout(this);
tl.setGravity(Gravity.CENTER);
TableRow firstRow = new TableRow(this);
TableRow secondRow = new TableRow(this);
But It doesn't work. Rows are centered but only vertically... How can I do?
Thanks in advance.
Edit:
TableLayout tl = new TableLayout(this);
tl.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, Gravity.CENTER_HORIZONTAL));
TableRow firstRow = new TableRow(this);
TableRow secondRow = new TableRow(this);
firstRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.FILL_PARENT, Gravity.CENTER));
secondRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.FILL_PARENT, Gravity.CENTER));

You should set a LayoutParams for the TableLayout and set the gravity on it:
tl.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL) );

Related

How to create table structure in Android programmatically not in xml file

I am creating an Android app for online shopping. I just want to generate invoice after payment in table format programmatically. Please guide me to create a table view in java program of Android.
This is how you create a Table Layout Programmatically for both Linear Layout and Relative Layout :
-------------------------------------------------------------------------
1) Linear Layout
TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams rowParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
TableLayout tableLayout = new TableLayout(context);
tableLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
TableRow tableRow = new TableRow(context);
tableRow.setLayoutParams(tableParams);
TextView textView = new TextView(context);
textView.setLayoutParams(rowParams);
tableLayout.addView(tableRow);
tableRow.addView(textView);
-------------------------------------------------------------------------
2) Relative Layout
TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams rowParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
TableLayout tableLayout = new TableLayout(context);
tableLayout.setLayoutParams(new Relative.LayoutParams(Relative.LayoutParams.WRAP_CONTENT, Relative.LayoutParams.WRAP_CONTENT));
TableRow tableRow = new TableRow(context);
tableRow.setLayoutParams(tableParams);
TextView textView = new TextView(context);
textView.setLayoutParams(rowParams);
tableLayout.addView(tableRow);
tableRow.addView(textView);
-------------------------------------------------------------------------

dynamically added table is not displayed in android

there is something going wrong , such that firstly i created separately xml file for table row ,it did not work, then i created table row and text view programatically, again it is not displayed when i run , i can not get if what is wrong with my code
table = (TableLayout) findViewById(R.id.goodsTable);
for (int i = 0; i < attrName.length; i++) {
TableRow tr = new TableRow(getApplicationContext());
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
TextView atName = new TextView(getApplicationContext());
atName.setText(attrName[i]);
tr.addView(atName);
table.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
}
table.requestLayout();
Try This it may be help to you
for (int i = 0; i < attrName.length; i++) {
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
TextView atName = new TextView(this);
atName.setText(attrName[i]);
atName.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
tr.addView(atName);
table.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
}
You must apply LayoutParam for TextView and for create new Object for TableRow or TextView you should use Activity Context not ApplicationContext

Width column tablelayout dynamically

I have a problem regarding not using XML in the TableLayout.
Someone has already asked the same as I do but the answer provided has been unable to help me.
how to set width of column dynamically in android tablelayout?
Right now I have this
TableLayout tl = (TableLayout) findViewById(R.id.TableLayout1);
/* Create a new row to be added. */
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_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.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
/* Add Button to row. */
tr.addView(b);
Button a = new Button(this);
a.setText("Dynamic Button");
a.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
/* Add Button to row. */
a.setGravity(2);
tr.addView(a);
But I fail to see how changing
tr.setLayoutParams
Will do the job of making for example the first button column 70% and the other button the 30%
You can create an linear layout with weight and keep those two buttons inside linear layout and then set linear to your table row.
Check below code:
TableLayout tl = (TableLayout) findViewById(R.id.TableLayout1);
/* Create a new row to be added. */
TableRow.LayoutParams params = new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
TableRow tr = new TableRow(this);
tr.setLayoutParams(params);
params = new TableRow.LayoutParams(0,
TableRow.LayoutParams.WRAP_CONTENT);
LinearLayout layout = new LinearLayout(this);
params.weight = 1;
layout.setLayoutParams(params);
layout.setBackgroundColor(Color.WHITE);
layout.setWeightSum(1);
/* Create a Button to be the row-content. */
LinearLayout.LayoutParams chiledParams = new LinearLayout.LayoutParams(0,
LinearLayout.LayoutParams.WRAP_CONTENT);
chiledParams.weight = (float) 0.7;
Button b = new Button(this);
b.setText("Button");
b.setLayoutParams(chiledParams);
/* Add Button to row. */
LinearLayout.LayoutParams chiledParams1 = new LinearLayout.LayoutParams(0,
LinearLayout.LayoutParams.WRAP_CONTENT);
chiledParams1.weight = (float) 0.3;
Button a = new Button(this);
a.setText("Button");
a.setLayoutParams(chiledParams1);
layout.addView(b);
layout.addView(a);
tr.addView(layout);
tl.addView(tr);
you need setLayoutParams to your widget like following code:
TableRow.LayoutParams tlp = new TableRow.LayoutParams(width,heigh);
b.setLayoutParams(tlp);
TableRow.LayoutParams tlp1 = new TableRow.LayoutParams(width/3,heigh/3);
a.setLayoutParams(tlp1);

Android Adding more than one TextView to a TableRow Programmatically

I have a LinearLayout inside a FrameLayout for tab purposes. And I'm trying to add TableRow's to the LinearLayout in the code.
LinearLayout testLayout = (LinearLayout)findViewById(R.id.testLayout);
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
This gets my LinearLayout and creates a TableRow to the specifications I want. I know this part is working.
TextView textOne = new TextView(this);
textOne.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
textOne.setText("One");
TextView textTwo = new TextView(this);
textTwo.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
textTwo.setText("Two");
Here I make my two TextViews with no problem.
tableRow.addView(textOne);
tableRow.addView(textTwo);
testLayout.addView(tableRow, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
Here is where I assume everything goes wrong. What happens is it only shows the textTwo. I don't know why it's not showing both of them in order like a normal TableRow would in XML. And I repeat, this must be done in code.
Please help, thank you.
Have you imported this import android.widget.TableRow.LayoutParams
Below code works for me
TableLayout tl = (TableLayout) findViewById(R.id.spreadsheet);
TableRow tr = new TableRow(this);
LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
tr.setLayoutParams(lp);
TextView tvLeft = new TextView(this);
tvLeft.setLayoutParams(lp);
tvLeft.setBackgroundColor(Color.WHITE);
tvLeft.setText("OMG");
TextView tvCenter = new TextView(this);
tvCenter.setLayoutParams(lp);
tvCenter.setBackgroundColor(Color.WHITE);
tvCenter.setText("It");
TextView tvRight = new TextView(this);
tvRight.setLayoutParams(lp);
tvRight.setBackgroundColor(Color.WHITE);
tvRight.setText("WORKED!!!");
tr.addView(tvLeft);
tr.addView(tvCenter);
tr.addView(tvRight);
tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tl.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
I think you want to create your TextView dynamically and should're getting error "removeView () parent." Here is a good solution for that:
TableView tlSkills = (TableView) findViewById(R.id.myTableView);
if(listSkills.size() > 0) {
TableRow tableRow = new TableRow(getContext());
int i = 0;
for (Skills s : listSkills) {
TextView textView = new TextView(getContext());
textView.setText("" + s.getName());
tableRow.addView(textView);
if(i > 0) {
tlSkills.removeView(tableRow);//this is to avoid the error I mentioned above.
}
tlSkills.addView(tableRow);
i++;
}
}

TableLayout items on the left and right

hy!
I want to have a TextView on the left and the right. But both is on the right. all settings should be done by code.
Another quest:
How to add´a horizantal line? by making a nother TableRow?
Photo:
My code:
TableLayout tl = (TableLayout)findViewById(R.id.tl);
TableRow tr = new TableRow(this);
TextView tv = new TextView(this);
tv.setText("Test");
tv.setGravity(Gravity.LEFT);
TextView tv2 = new TextView(this);
tv2.setGravity(Gravity.RIGHT);
tv2.setText("Test");
tr.addView(tv);
tr.addView(tv2);
tl.addView(tr);
Try this stuff,
TableLayout tl = (TableLayout)findViewById(R.id.tl);
TableRow tr = new TableRow(this);
TextView tv = new TextView(this);
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
tv.setText("Test");
tv.setGravity(Gravity.LEFT);
TextView tv2 = new TextView(this);
tv2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
tv2.setGravity(Gravity.RIGHT);
tv2.setText("Test");
tr.addView(tv);
tr.addView(tv2);
tl.addView(tr);
setContentView(tl);

Categories

Resources