I want to add image buttons next to each other, and once it reaches the end of the LinearLayout it should go into the next row.
Note : API Level 10
What I probably need to do, but I don't know how :
- Calculate end of parent width
- Add top margin value calculated by row number
- Set image button to parent's left
If there is a better way, please post it.
Here is my testing code :
LinearLayout layout = new LinearLayout(this);
LinearLayout.LayoutParams paramsLO = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
paramsLO.setMargins(0, 0, 0, 0);
paramsLO.height = 150;
paramsLO.width = 150;
for (int i = 0; i < 20;i++)
{
ImageButton imgBtn = new ImageButton(this);
imgBtn.setBackgroundColor(Color.TRANSPARENT);
imgBtn.setImageResource(R.drawable.placeholder);
imgBtn.setAdjustViewBounds(true);
layout.addView(imgBtn,paramsLO);
}
layout.setGravity(Gravity.LEFT | Gravity.CENTER_HORIZONTAL);
addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));