I want to achieve the following effect:
Unfortunately, this is what I do now:
How can I add spaces between ImageView to get the same effect as in the first image? This is my code:
public class MainActivity extends AppCompatActivity {
private int totalValue = 2000;
private int currentValue = 180;
private int rowsNumber = 10;
private int columnsNumber = 10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TableLayout tableLayout = findViewById(R.id.tableLayout);
for (int i = 0; i < 10; i++) {
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
for (int j = 0; j < columnsNumber; j++) {
ImageView imageView = new ImageView(this);
imageView.setImageResource(this.getCellDrawableId(i, j, totalValue, currentValue));
tableRow.addView(imageView, j);
}
tableLayout.addView(tableRow, i);
}
}
private int getCellDrawableId(int i, int j, int totalValue, int currentValue) {
return R.drawable.test;
}
}
Use
TableRow.LayoutParams lp = new TableRow.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
and it will work.
TableRow.LayoutParams lp = new TableRow.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.leftMargin = margin;
lp.topMargin = margin;
lp.rightMargin = margin;
lp.bottomMargin = margin;
imageView.setLayoutParams(lp);
Or you can use imageView.setPadding(padding, padding, padding, padding);
.
Cheers.