1

I want to create a layout where buttons are added dynamically.In this the no of buttons to be added are decided on run time i.e depending upon the number of buttons return by server i want to add buttons.

 for (int k = 1; k < 100; k++) {
            TableRow row = new TableRow(this);

            innerloop:
           for (int  l = 1; l <4; l++) {
                  btn = new Button(this);
                  TableRow.LayoutParams tr= new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
                  layout.setWeightSum(12.0f);
                  tr.weight=0;
                    btn.setLayoutParams(tr); 
                  btn.setTextColor(a);
//                      btn.setLayoutParams(params);
                  btn.setHeight(150);
//                      Log.v("y", "how much"+size.x+"  "+size.y);
                  btn.setWidth(150);
                  btn.setId(idb);
                  btn.setOnClickListener(this);
                  btn.setText("Button " + idb);
//                      Log.v("idb", "created"+" "+btn.getId());
                  row.addView(btn);
                  }
}
Harish Koona
  • 189
  • 4
  • 16

3 Answers3

0
  Button myButton = new Button(this);
  myButton.setText("Push Me");

    LinearLayout ll = (LinearLayout)findViewById(R.id.buttonlayout);
    LayoutParams lp = new 
    LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
    ll.addView(myButton, lp);
Sino
  • 886
  • 7
  • 21
0

I hope that you are going to add the button in the container layout.

  LinearLayout containerLayout = (LinearLayout) findViewById(R.id.parentLayout);
        Button btnDynamic = new Button(YourActivity.this);
        btnDynamic.setText("Click to get values");
        LinearLayout.LayoutParams layoutParams = new  LinearLayout.LayoutParams(
                height,width);
        layoutParams.setMargins(5, 5, 5, 5); // left, top, right, bottom
        btnDynamic.setLayoutParams(layoutParams);
        containerLayout.addView(btnDynamic);

thats it, You are done.

Rethinavel
  • 3,912
  • 7
  • 28
  • 49
0
int flag=4;//some value returened by server.
TableLayout tl = (TableLayout)findViewById(R.Id.tl);
Button btn[] = new Button[flag];
TableRow row[] = new TableRow[flag];
TableRow.LayoutParams tr= new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);

for(int i = 0; i<flag;i++)
{
                  btn[i]= new Button(this);    
                  row[i]= new TableRow(this);   

                  row[i].setLayoutParams(tr); 
                  btn[i].setTextColor(Color.RED);
                  btn[i].setHeight(150);
                  btn[i].setWidth(250);
                  btn[i].setOnClickListener(new OnClickListener() 
                  {
                    @Override
                    public void onClick(View v) 
                    {
                      //do something here.
                    }
                  });
                  btn[i].setText("this is Button " + flag);
                  row[i].addView(btn[i]);
                  tl.addView(row[i]);
}
krishna
  • 4,069
  • 2
  • 29
  • 56