0

I need to place a dinamic number of buttons in some rows. The number of buttons per row and the size should fit to any screen width.

LinearLayout llh = new LinearLayout(this);
llh.setOrientation(LinearLayout.HORIZONTAL);
for(int i=1; i<=nl; ++i) {
    Button b = new Button(this);
    b.setText(String.valueOf(i));
    if(i>ul) {
        b.setFocusable(false);
        b.setEnabled(false);
    }
    llh.addView(b);
}

The problem with this piece of code is that for example, my nl test value is 10, and this only displays 6 buttons, all in the same row, and the last one is smaller than the others.

I need them to stack vertically, like, when there's no space for another button, a new row is created and the rest of the buttons go in there.

Thanks in advance.

Bruno Tavares
  • 450
  • 1
  • 4
  • 18
  • Also why not let android to make the same width by setting layout_weight to same value.See http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html – Frank ZHENG Jun 30 '14 at 01:11
  • Wouldnt that cause to the buttons be too big or small depending on the screen size? – Bruno Tavares Jun 30 '14 at 01:25

3 Answers3

0

Sounds like you are talking about a vertical FlowLayout, where newly added views are stacked vertically until there is no more room, then a new column is started.
Unfortunately Android does not already have a FlowLayout, but you can make your own. Check out this answer by Romain Guy "How can I do something like a FlowLayout in Android?", and watch the video of his talk where he describes how to create one. I learnt a great deal about creating custom layouts by watching this several times until I understood it.

Community
  • 1
  • 1
Sound Conception
  • 5,263
  • 5
  • 30
  • 47
0

If the width of the screen is less than a certain value, set the weight property to 1 for all buttons. And if the screen width is large enough to fit all your buttons properly, go with the default.

I cannot post the code now as I am far away from my PC.

Abhishek Verma
  • 366
  • 2
  • 13
0

This is what I came up with. It's not as pretty as I tought but it gets the job done. Buttons will have fixed size, but that shouldn't be much of a problem. Thanks for all your help :)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top|center_horizontal"
android:orientation="vertical"></LinearLayout>


    Display display = getWindowManager().getDefaultDisplay();
    Point p = new Point();
    display.getSize(p);

    int buttonSize = 120;
    int n = p.x/buttonSize-1;

    LinearLayout llv = (LinearLayout)findViewById(R.id.container);
    LinearLayout llh = null;

    for(int i=0; i<nl; ++i) {
        Button b = new Button(this);
        if(i%n==0 || i==0) {
            llh = new LinearLayout(this);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            llh.setLayoutParams(params);
            llh.setOrientation(LinearLayout.HORIZONTAL);
            llv.addView(llh);
        }
        b.setText(String.valueOf(i+1));
        b.setWidth(buttonSize);
        if(i>ul) {
            b.setFocusable(false);
            b.setEnabled(false);
        }
        llh.addView(b);
    }
Bruno Tavares
  • 450
  • 1
  • 4
  • 18