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 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
Ok, I have got it to work.
The steps are the following: First inflate the xml layout, ie,
Then instantiate the container object from the xml layout into a ViewGroup class, ie,
Then create a linearLayout object, create and add onto that any controls needed, add the linearLayout to the container object and use setContentView on the view object, ie,
You can do this quite easy by setting an id on the layout on which you want to add views to. Say your main.xml look like this:
Lets assume that you want to add your additional views to the
LinearLayout
with idid/container
. In youronCreate
method you could retrieve that object for later use:Now you are all set to add other views to your container
ViewGroup
:In the
getButtons
method you need to create yourLinearLayout
containing the buttons you need. Either you do this programmatically or by inflating a view defined in an XML file. SeeLayoutInflater.inflate
.just try this:
now create button dynamically like this
now if you want to add onether linearlayout then add it below button then
I see the error u r doing here
You r taking the layout as Linearlayout object, you should take the LinearLayout id
Try this
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