3

I want to add multiple buttons dynamically through the code on button click, I searched many previous posts which shows to add single button, but I need multiple ones.

Attached is the sample code.

   public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button b1 = (Button)findViewById(R.id.button1);
        b1.setOnClickListener(new View.OnClickListener() {      
            @Override
            public void onClick(View v) {               
                AddAll();                   
            }
        });
    }
    public void AddAll() {
        final RelativeLayout rl = (RelativeLayout)findViewById(R.id.rel);
        final Button btn = new Button(this);
        for(int i=0;i<4;i++)
        {
            rl.addView(btn); 
            btn.setText("hello");
            btn.setWidth(320);
            btn.setHeight(40);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

Please help regarding the same. However adding single button is working fine, but I need to add many buttons one below the other.

picciano
  • 22,341
  • 9
  • 69
  • 82
bharath
  • 953
  • 4
  • 17
  • 30

2 Answers2

3
public void AddAll() {
final RelativeLayout rl = (RelativeLayout)findViewById(R.id.rel);

for(int i=0;i<4;i++)
{
    final Button btn = new Button(this);
    rl.addView(btn); 
    btn.setText("hello");

    btn.setWidth(320);
    btn.setHeight(40);
}
//////////////////////////////////////////////////////////
}

For more detail :-

How do I programmatically add buttons into layout one by one in several lines?

or

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ScrollView scrollView= new ScrollView(this);
    LinearLayout mainLayout = new LinearLayout(this);
    mainLayout.setOrientation(LinearLayout.VERTICAL);                
    for (int i=0; i<10; i++){
        LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.HORIZONTAL);
        ll.setTag(i);                            
        TextView tv=new TextView(this);
        tv.setText("Row " + i);            
        ll.addView(tv);
        Button b = new Button(this);
        b.setTag(i);
        b.setText("Button " + i);            
        ll.addView(b);                    
        mainLayout.addView(ll);
    }
    scrollView.addView(mainLayout);
    setContentView(scrollView);
}
Community
  • 1
  • 1
duggu
  • 37,851
  • 12
  • 116
  • 113
  • Thanks for the help, no errors now, but still i am able to view only one button, may be all the 4 buttons are overlapping?? can i make it one below the other?? – bharath Mar 12 '13 at 10:49
  • hope so once u have to use linear layout to see u going right way or not. ohk – duggu Mar 12 '13 at 10:51
  • checked with linear layout as well, not displaying 4 different buttons which i want! Any other suggestions? – bharath Mar 12 '13 at 10:55
  • Ok, making both linear view workk, but is it not possible to do them in relative layout?? – bharath Mar 12 '13 at 11:05
  • how?? Any major modifications?? Just replacing them with Relative Layout is not working, plz suggest the right way! – bharath Mar 12 '13 at 11:07
-1
public void AddAll() {
  LinearLayout layout = (LinearLayout) findViewById(R.id.linear);
        layout.setOrientation(LinearLayout.HORIZONTAL); 

            for (int j = 0; j < 4; j++ ){
                Button btnTag = new Button(this);
                btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                btnTag.setText("Button " + j);
                layout.addView(btnTag);
            }
        }
VKSingh
  • 452
  • 6
  • 17