First, assign an id to your FrameLayout in the XML:
<FrameLayout android:id="@+id/MyFrameLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" />
Then, you can access any inflated Views in your Activity
after calling setContentView()
by using findViewById()
in conjunction with the automatically generated R.java
:
setContentView(R.layout.somelayout);
FrameLayout layout = (FrameLayout) findViewById(R.id.MyFrameLayout);
findViewById()
will search recursively, so regardless where your FrameLayout
is, 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
:
Button button = new Button(this);
layout.addView(button);