2

how can I create a button programmatically

Button x = new Button (this);

but this button I add it in next available position. Now, if I do this and reach four created buttons, the fifth one will be outside the screen. How can I let the fifth one check if there is enough space to the right, if not, then the button would be created below the rest.

I tried grid layout and increase the column number but it is not good as I want to support Android 7.

user3549071
  • 117
  • 5

4 Answers4

1

Try to use scrollView in the layout

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true">
Ling QI
  • 17
  • 6
  • Is there another way to approach it? Because I wanna fill the whole screen, so If I would create 20 squares, I would have 5 rows, each with 4 buttons and all of this is done programmatically. – user3549071 Jun 15 '16 at 23:23
1

Wrap them up in a vertical LinearLayout, then add them the weight attribute:

Button button = new Button(this);
button.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
Community
  • 1
  • 1
Evin1_
  • 12,292
  • 9
  • 45
  • 47
  • nice way of thinking, but it is not solving the problem. It is just extending the amount of buttons I could add before eventually they would be out of screen, using **no** scrollview. Can't I detect when they would be out of screen or too small in case of using weight and then create the further buttons to the right of these? – user3549071 Jun 16 '16 at 00:03
1

Did you tried to create or search for FlowLayout. There is so many Github repos for it .

And i will suggest you to look this question : How can I do something like a FlowLayout in Android?

If you go to Github and search this, you can find examples : Github Flow Layout

Also you can search for TagView too for learning how to create custom view and add it dynamically : Tag View

Community
  • 1
  • 1
Yasin Kaçmaz
  • 6,573
  • 5
  • 40
  • 58
1

It's hackish but it works?The Main LinearLayout in the xml is in vertical orientation. So it checks the total width of all the buttons you've generated against screen width and if it's more than or equal it will create a new linearlayout with horizontal orientation and continue putting buttons in.

This just takes in number of buttons you want to create.

private void generateButton(int noOfButton){
        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        totalButtonWidth = 0;
        while(noOfButton!= 0){
            DisplayMetrics metrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(metrics);
            int phoneWidth = metrics.widthPixels;
            button = new Button(this);
            button.setLayoutParams(new LinearLayout.LayoutParams(244,200));
            totalButtonWidth += 244;
            Log.i(TAG,"totalWidth"+totalButtonWidth);
            if(totalButtonWidth>=phoneWidth){
                totalButtonWidth = 244;
                linearLayoutMain.addView(linearLayout);
                linearLayout = new LinearLayout(this);
                linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT));
                linearLayout.setOrientation(LinearLayout.HORIZONTAL);
            }
            linearLayout.addView(button);
            noOfButton--;
        }
        linearLayoutMain.addView(linearLayout);
    }
Skyler
  • 97
  • 3