I have an android xml layout, main.xml. I would like to add controls to this layout at runtime (I would like to add a series of additional linear layouts that contain buttons controls). Can I do that and if yes, how?
Thanks
I see the error u r doing here
LinearLayout mainLayout = (LinearLayout) findViewById(R.layout.main);
You r taking the layout as Linearlayout object, you should take the LinearLayout id
Try this
LinearLayout lnr = (LinearLayout) findViewById(R.id.LinearLayout01);
Button b1 = new Button(this);
b1.setText("Btn");
lnr.addView(b1);
You can add controls programmatically if you want in your code, or even another XML with a View and an Inflater.
Here you can read the basics: http://developer.android.com/guide/topics/ui/declaring-layout.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With