Written By:
Brian Killion
- Published
Thursday, March 24, 2011
The Android
platform is very interesting and presents new challenges to figure out
how to do things that I have done many times in the past on other
platforms. One of those things is adding controls (views) to a form
(layout of some sort) from Java code without setting the control up
first in the XML markup.
I am fairly new to the Android environment and don't claim to be an
expert. I am learning as well but figured I would share some of the
things I have learned here so other developers can benefit from it.
Here is how I did it
In order to create and add a control to your
Activity View using only Java code, you need to do the following:
In the xml markup of the layout, add an id to the node to which you would like to add controls dynamically.
In the Java Activity
After chaining to the super class...
- Create an instance of the control object
- Set the attributes of your control
- Set
the layout parameters for the control. The important thing here is
that the type of the layout parameters needs to be the same as the layout
element you are using to add the control (see the code below)
- Inflate the layout you would like to use for this activity into a
variable. This needs to be the type of your root layout node (ie:
ScrollView, LinearLayout, etc.)
- Using this variable, find
the the view by ID in the layout where you would like to put the
control. The important thing here is to make sure that you cast it to
the type that the ID is assigned.
- Add your control object to the view you just found
- Set the Activity's view with the inflated view (NOT the R reference to the XML view)
Here
is a sample bit of code to help out. Here I am adding a button to my
main layout (main.xml) but am placing it in a
TableRow element (in the XML). I
have given the
TableRow element the attribute
android:id="@+id/trStuff"
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Create a new button instance
Button myButton = new Button(this);
//Set the attributes
myButton.setText("Push Me");
//Set the layout parameters
myButton.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.FILL_PARENT));
//inflating the main layout. It is a TabHost as the top most element so cast it to that type
TabHost layout = (TabHost) View.inflate(this, R.layout.main, null);
//find the view by the trStuff id; Remember to cast the result of the find to the element type that will
//hold the new control
TableRow vTblRow = (TableRow)layout.findViewById(R.id.trStuff);
//add the button to the view
vTblRow.addView(myButton);
//set the layout using the inflated view
setContentView(layout);
//do other things below like add a click listener for the button, etc.
}
There may be better ways to do this such as inflating the view and then
passing it to a helper method that does the button creation and adding
it to the view.
Brian
A little background
For those who don't know my background, I have been programming for 15
years and am currently teaching programming classes at the local
community college. One of the things I enjoy about teaching is it keeps
me learning things that are on the cutting edge because students are
curious and ask questions about these topics. As a result I have started to teach Android development.