I’m using setContentView(R.layout.somelayout); to load an XML to my activity.
On this layout, there is a FrameLayout, I want to insert more objects in it dynamically, as an example, more Button Objects.
I haven’t found some getCurrentView() method yet… So, how can I get the current Layout from the activity?
First, assign an id to your FrameLayout in the XML:
Then, you can access any inflated Views in your
Activityafter callingsetContentView()by usingfindViewById()in conjunction with the automatically generatedR.java:findViewById()will search recursively, so regardless where yourFrameLayoutis, it will be found (as long as there isn’t a duplicate id somewhere else in the XML, which you should avoid).From there, just create the Button dynamically and add it as a child of the
FrameLayout: