0

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));
Marc Uberstein
  • 12,501
  • 3
  • 44
  • 72

2 Answers2

1

Have you tried the FlowLayout
here is a good lib flowlayout, simple and easy to use.

JafarKhQ
  • 8,676
  • 3
  • 35
  • 45
0

Please have a look at the ApiDemos -> Animation -> LayoutAnimations.

I think this is exactly what you are looking for.

See these links if you don't know how to install the ApiDemos. https://stackoverflow.com/a/4467915/2538428 http://developer.android.com/tools/samples/index.html

Hope that helps...

Community
  • 1
  • 1
André Diermann
  • 2,725
  • 23
  • 28