1

I tried the below code to create multiple buttons through programatically but it creates a single button as per my application i need to create a button based on input. For example if the input is 3 means i need to create three buttons in a layout. For your reference i attached the sample image and my code.

for (int i = 0; i < array_of_btn_input.size(); i++) {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);

            LinearLayout layout = new LinearLayout(getApplicationContext());
            LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            layout.setOrientation(LinearLayout.VERTICAL);
            layout.setLayoutParams(params);
            Button button1 = new Button(getApplicationContext());
            button1.setLayoutParams(params1);
            button1.setText("button");

            layout.addView(button1);
            main_layer.addView(layout);
}

enter image description here

4
  • 1
    What type of view is the main layout? It's possible it's just stacking the buttons on top of each other. Commented Nov 15, 2012 at 15:12
  • 1) Why do you need a LinearLayout for each button ? Commented Nov 15, 2012 at 15:13
  • 2) What is the global container ? Commented Nov 15, 2012 at 15:13
  • Because of relative layout you have placed them on top of eachother, i wrote answer below Commented Nov 15, 2012 at 15:26

4 Answers 4

1

if in your example, your global container (main_layer) is a relativelayout or a frame layout, you have placed them on top of eachother. So you can't see the one on the back order.

Try this pls,

LinearLayout main_layer= (LinearLayout) findViewById(id.main_layer);


for (int i = 0; i < 10; i++) 
{
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);

LinearLayout layout = new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);

layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(params);
Button button1 = new Button(getApplicationContext());
button1.setLayoutParams(params1);
button1.setText("button");

layout.addView(button1);
main_layer.addView(layout);
}
Sign up to request clarification or add additional context in comments.

Comments

0

You are creating multiple new linear layouts within the for loop. Linear layout need to be taken out of the for loop and just try adding the buttons in it.

1st run of for loop creates a new linear layout and adds a button

2nd run of for loop creates a new linear layout and adds a button on the top of previous added layout

Comments

0

Here you are creating a new LinearLayout per button... Try something more like...

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.WRAP_CONTENT,
    LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout layout = new LinearLayout(getApplicationContext());
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(params);

for (int i = 0; i < array_of_btn_input.size(); i++) {
    LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);

    Button button1 = new Button(getApplicationContext());
    button1.setLayoutParams(params1);
    button1.setText("button");

    layout.addView(button1);

}

main_layer.addView(layout);

Comments

0

Try this

LinearLayout llParent = (LinearLayout) findViewById(R.id.llParent);
llParent.removeAllViews();
for (int i = 0; i < array_of_btn_input.size(); i++) {
    BSView b = new BSView(this, new SimpleThumbBean(i + 1, bsList
            .get(i).getLabel(), bsList.get(i).getThumbUrl()));
    LinearLayout.LayoutParams llDynamic = new LinearLayout.LayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
    llDynamic.weight = 1f;
    llParent.addView(b, llDynamic);
}

llParent is a defined Layout and make sure you call removeAllViews() before adding layouts dynamically into it.

BSView is my own custom View. You can set BSView into ordinary Button as you need it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.