I’m trying to create a chess game board with 8×8 buttons. I’m using nested LinearLayouts. The problem is that there’s a padding between each row of ImageViews that I can’t get rid of. This is what I have sofar, the board should be square, but isn’t:

public class BoardLayout extends LinearLayout {
public BoardLayout(Context context) {
super(context);
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
setLayoutParams(parms);
setOrientation(LinearLayout.VERTICAL);
setPadding(0, 0, 0, 0);
for (int i = 0; i < 8; i++) {
LinearLayout row = new LinearLayout(context);
row.setLayoutParams(parms);
row.setOrientation(LinearLayout.HORIZONTAL);
row.setPadding(0, 0, 0, 0);
for (int j = 0; j < 8; j++) {
ImageView button = new ImageView(context);
button.setLayoutParams(parms);
button.setImageResource(R.drawable.icon);
button.setAdjustViewBounds(true);
button.setPadding(0, 0, 0, 0);
row.addView(button);
}
addView(row);
}
setSquare();
}
public void setSquare() {
int size = Math.min(getWidth(), getHeight());
// setHeight(size); // Not Function, but this is what I need
// setWidth(size); // Not Function, but this is what I need
// also doesn't work
setLayoutParams(new LinearLayout.LayoutParams(size, size, 1.0f));
}
}
Solution:
As suggested TableLayout is better for what I’m trying to do. So the main class should be changed to a TableLayout and the nested LinearLayouts to TableRows.
But that alone didn’t fix it, I also had to call the following in TableLayout:
setShrinkAllColumns(true);
setStretchAllColumns(true);
and for each item inserted, I had to also call:
setAdjustViewBounds(true);
1 Answer