How to set padding to columns in table layout dynamically - android

I am trying to create a table layout with buttons dynamically. I am able to get the table layout with buttons. bt i need padding between buttons. How i can get programatically.
I tried following code bt
private void showCowsTblField()
{
for (int row = 0; row < numberOfRowsInField-1; row++)
{
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT ));
for (int column = 0; column < numberOfColumnsInField -1; column++)
{
blocks[row][column].setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
blocks[row][column].setPadding(blockPadding, blockPadding, blockPadding, blockPadding);
tableRow.addView(blocks[row][column]);
tableRow.setPadding(blockPadding, blockPadding, blockPadding, blockPadding);
}
tblCows.addView(tableRow,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
}
}
Please let me know.... Thanks.

i found answer, by using setmargin to the layout params applied to button Set margins in a LinearLayout programmatically
LayoutParams layoutParams = new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(30, 20, 30, 0);

You've been set padding of the edges of the TableRow, not between its elements.
You can set padding of every individual view in the TableRow by doing this:
for(int i = 0; i < tableRow.getVirtualChildCount(); i++){
tableRow.getVirtualChildAt(i).setPadding(blockPadding, blockPadding, blockPadding, blockPadding);
}
This is really stupid, but it is the best I can think of right now.

Other solutions may work, but the easiest and best solution I have found so far is to add more "blank" columns where you need the spacing. If you are doing the buttons dynamically just add a text view between the buttons dynamically. I am only suggesting this if you are trying to use a TableLayout though.

Related

Android how to create two table layouts in Activity?

Im trying to create two table layouts through activity..
I already have one table layout but how to set through activity?
I know to do it through xml but want to do it programatically..
Please Help
Check this answer and another example here
Just like in xml, you will create a TableLayout, provide params and add rows with your own UI in it.
Take one linear layout(or relative layout) in in your xml get it reference by findViewById() in onCreate() method of your activity.after that create table dynamically and add it to the linear layout.I create a method to do so . ex-
LinearLayout linear= (LinearLayout ) findViewById(R.id.linear);
//call method to add the tablelayout.
linear.addView(createtable(3,5));
private TableLayout createtable(int requiredcolumn, int requiredrow) {
TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT, 1f);
TableLayout.LayoutParams rowParams = new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1f);
//for border
rowParams.setMargins(2, 2, 2, 2);
TableRow.LayoutParams itemParams = new TableRow.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1f);
TableLayout tableLayout = new TableLayout(MainActivity.this);
tableLayout.setLayoutParams(tableParams);
tableLayout.setBackgroundColor(Color.WHITE);
for (int row = 0; row < requiredrow; row++) {
TableRow tableRow = new TableRow(MainActivity.this);
tableRow.setLayoutParams(rowParams);
for (int column = 0; column < requiredcolumn; column++) {
Random color = new Random();
int randomColor = Color.argb(255, color.nextInt(256),
color.nextInt(256), color.nextInt(256));
TextView textView = new TextView(MainActivity.this);
textView.setLayoutParams(itemParams);
textView.setBackgroundColor(randomColor);
tableRow.addView(textView);
}
tableLayout.addView(tableRow);
}
return tableLayout;
}

Padding between dynamically created TableRows

I have created a TableLayout and then I created TableRow dynamically in my java code, and added some buttons in the form of an 8x8 grid. But I want to reduce the space between the buttons. I tried setting LayoutParam for the TableRow , but when I do this , the output shows just a blank screen. Here's my code:
LayoutParams param= new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
field=new Button[8][8];
tb=new TableLayout(this);
param.setMargins(10, 2, 10, 2);
for (int i = 0; i < field.length; i++) {
TableRow current=new TableRow(this);
for (int j = 0; j < field[i].length; j++) {
Button button=new Button(this);
field[i][j]=button;
button.setWidth(40);
button.setHeight(40);
button.setLayoutParams(param);
current.addView(button);
}
tb.addView(current);
}
t.addView(tb);
But when I don't write button.setLayoutParams(param)
I get an output like this:
which is the normal output except that I want the space between the buttons reduced.
The spacing you're seeing is padding built into the standard Android button background asset. You can see that your layout is correct by turning on "Show Layout Bounds" in Settings > Developer Options. You just need to make your own button asset, or if a simple color is all that is needed, then just set the button background to be a color.
In the param.setMargins() call, use negative values as necessary to get past what seems to be some natural spacing. You will also want to give the same layout margins to the table layout, and use WRAP_CONTENT for both the width and height. I am not sure if variable "t" is needed as I created the buttons without it using a TableLayout in an XML file. (I also did a 5x5 grid to fit onto my screen.)
LayoutParams param= new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
field=new Button[5][5];
tb=new TableLayout(this);
// these are the two important changes
param.setMargins(-5, -5, -5, -5);
tb.setLayoutParams(param);
for (int i = 0; i < field.length; i++) {
TableRow current=new TableRow(this);
for (int j = 0; j < field[i].length; j++) {
Button button=new Button(this);
field[i][j]=button;
button.setWidth(40);
button.setHeight(40);
button.setLayoutParams(param);
current.addView(button);
}
tb.addView(current);
}
t.addView(tb);

Adding TableRows dynamically in Android doesn't work

I got a little problem with adding tablerows to a table layout in android.
Here's my code:
int z = 0;
for(String detail: details){
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
//odd / even
if(z%2 == 0)
tr.setBackgroundColor(Color.parseColor("#F5F5F5"));
//set detail text
TextView detailText = new TextView(this);
RelativeLayout.LayoutParams dtlp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
detailText.setTextSize(16.0f);
detailText.setTextColor(Color.parseColor("#333333"));
detailText.setPadding(20, 10, 20, 10);
detailText.setLayoutParams(dtlp);
detailText.setText(detail);
tr.addView(detailText);
detailsTable.addView(tr);
++z;
}
I cannot figure out where the problem is. The detail textview is being set but the tablerows won't show up.
a. Try changing dtlp's "width parameter" to "RelativeLayout.MATCH_PARENT"
OR
b. Before For loop block create two layout params like this.
TableLayout.LayoutParams tlps=new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,TableLayout.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams trps=new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT);
In loop Replace
detailText.setLayoutParams(dtlp);
WITH
detailText.setLayoutParams(trps);
And replace
detailsTable.addView(tr);
with
detailsTable.addView(tr,tlps);
hope this helps.

Adding a dynamic TableLayout with borders in a Android App

I am creating a Android app. This Android App will have objects that are dynamic. These objects are Places with a Address or Lat/Long, and distance from current location, and a ETA. What I would like to do is add with objects on a TableLayout with borders, but I need to be able to dynamically add rows as the number of places increase.
I understand somewhat how to do this for a fixed hardcoded number of items on the xml, but what would be the best way when the number of objects is coming from the Activity.java file?
Below is a screenshot of the TableLayout I would like:
So the object would be a place with a address, distance and direction.
but I need to be able to dynamically add rows as the number of places increase.
This isn't difficult, when you have a new object append a TableRow with the data to the TableLayout.
I understand somewhat how to do this for a fixed hardcoded number of items on the xml, but what would be the best way when the number of objects is coming from the Activity.java file?
I don't think there is a best way (or what you consider best way). You either:
Insert fake views to act as dividers. This would be easier to implement visually but it will also increase the memory consumption of your app, with bad consequences if the number of rows is big. (1)
Or use drawables for the backgrounds to simulate the borders (like nine-patch images). This would be simpler then inserting additional views but you need a bit more talent to make it look well. (2)
Some examples for your image:
(1)
private static final int DIVIDER_SIZE = 2;
// rowsCount the number of rows to add to the TableLayout
private void buildOldSchool(TableLayout table, int rowsCount) {
View divider;
for (int i = 0; i < rowsCount; i++) {
TableRow row = new TableRow(this);
for (int j = 0; j < 7; j++) {
if (j % 2 == 0) {
divider = new View(this);
divider.setLayoutParams(new TableLayout.LayoutParams(
DIVIDER_SIZE, TableLayout.LayoutParams.MATCH_PARENT));
divider.setBackgroundColor(Color.GREEN);
row.addView(divider, new TableRow.LayoutParams(
DIVIDER_SIZE, TableRow.LayoutParams.MATCH_PARENT));
continue;
}
TextView tv = new TextView(this);
tv.setText("DX"); // dummy data
row.addView(tv, new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
}
divider = new View(this);
divider.setLayoutParams(new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT, DIVIDER_SIZE));
divider.setBackgroundColor(Color.GREEN);
if (i == 0) {
table.addView(divider);
divider = new View(this);
divider.setLayoutParams(new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT, DIVIDER_SIZE));
divider.setBackgroundColor(Color.GREEN);
}
table.addView(row);
table.addView(divider);
}
}
(2) or with images:
private void buildWithDrawables(TableLayout table, int rowsCount) {
for (int i = 0; i < rowsCount; i++) {
TableRow row = new TableRow(this);
row.setBackgroundResource(i == 0 ? R.drawable.firstrow
: R.drawable.normalrow);
for (int j = 0; j < 3; j++) {
TextView tv = new TextView(this);
tv.setBackgroundResource(j == 2 ? R.drawable.extra
: R.drawable.cell);
tv.setText("DX");
row.addView(tv, new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
}
table.addView(row);
}
}
Where the images are:
R.drawable.cell:
R.drawable.extra (a visually transparent drawable which replicates the nine-patch above):
R.drawable.normalrow:
R.drawable.firstrow:
Ignore my design skills.
If your foresee a large number of rows I would advise you to use a ListView, which you could pretty easy make it to look like a table with borders.
Couldn't figure out the vertical line, but something you can build upon
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ScrollView sv = new ScrollView(this);
TableLayout ll=new TableLayout(this);
HorizontalScrollView hsv = new HorizontalScrollView(this);
for(int i=1;i<5;i++) {
TableRow tbrow=new TableRow(this);
for(int j=1;j<=3;j++) {
TextView tv1=new TextView(this);
tv1.setText("Element :"+ i + "" + j);
tbrow.addView(tv1);
}
ll.addView(tbrow);
View v = new View(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5);
v.setLayoutParams(params);
v.setBackgroundColor(getResources().getColor(android.R.color.white));
ll.addView(v);
}
hsv.addView(ll);
sv.addView(hsv);
setContentView(sv);
}

How to set button background image to wrap content in a table layout dynamically

Im implementing tablelayout dynamically code below.
private void showCowsTblField() {
for (int row = 0; row < numberOfRowsInField - 1; row++) {
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
for (int column = 0; column < numberOfColumnsInField - 1; column++) {
LayoutParams layoutParams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
if (column == 0) {
textSno[row][column].setLayoutParams(layoutParams);
layoutParams.setMargins(0, 0, 0, 0);
tableRow.addView(textSno[row][column]);
} else {
blocks[row][column].setLayoutParams(layoutParams);
layoutParams.setMargins(1, 1, 1, 1);
tableRow.addView(blocks[row][column]);
}
tableRow.setBackgroundResource(R.drawable.bar_1);
}
tblCows.addView(tableRow, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}
}
here block is a class extends Button class,
in this class iam setting background image as
this.setBackgroundResource(R.drawable.edit_button);
Here problem is the button background image size(height) is changing to fit to tablerow height. which is not looking good.
How i can set button background image to actual size (wrapcontent) in table row.
Please let me know....
Thanks.........
See if that helps - http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
You can set it only if you use http://developer.android.com/reference/android/widget/ImageButton.html
Your code must look something like this:
class MyButton extends ImageButton{...}
this.setScaleType(scaleType);
I tried the following code in my class; which extends button class
this.setMaxHeight(26);
this.setMaxWidth(54);
solved issue.:)

Categories

Resources