Home Tutorials Hints & Tips

Getting Started in Android Development

Part 33 - Adding Views Programmatically and Testing the XML Parser

Continued from the previous tutorial, we will cover the testing of the XML parser. However, the main focus will be on the private printValues() method. As implied, this method prints the data retrieved for the weather.xml file. The difference here compared to previous tutorials with layouts is that the views are not pre-defined in XML. They will be created dynamically with java programming. The concept of programmatically creating views deserves some attention.

In our example we created a basic layout to display the XML data. Aside from the TextView heading there is a nested linear layout with horizontal orientation. Inside of this layout there are three TextViews that will hold the table headings for the city, temperature and weather. They each have a weight of 1 so that they are evenly spaced across the screen. It is this arrangement of a horizontally oriented, nested linear layout and associated TextViews that we will create programmatically to hold the actual XML data. We will do this for each of the three XML nodes to generate three rows of data.

It’s important to give the parent layout an ID as we will need to make changes to it in java.

LinearLayout ll1;

ll1 = (LinearLayout) findViewById(R.id.ll1);

If we focus on the printValues() method, the first task is to create the nested linear layout.

LinearLayout weatherData = new LinearLayout(this);

We now need to define layout parameters that will be applied to each of the three TextViews.

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT);

This is a complex piece of code, but it essentially means that each TextView will have a width of 0dp and a height of wrap_content. The width is set to 0dp as this is what is needed to ensure that the weight functions correctly. If you need more information on Android weight then take a look at my tutorial on this subject.

We can then apply a weight of 1 to the params object.

params.weight = 1;

Next set the orientation of the linear layout we just created.


The next task is to create the TextViews with the code below:

TextView cityText = new TextView (this);
TextView temperatureText = new TextView (this);
TextView weatherText = new TextView (this);

The layout parameters are the applied to each TextView.


The XML data is used to set the text of each TextView.


The following code is used to add each TextView to the linear layout:


Finally, so that the data actually can be seen, the new linear layout needs to be added to the parent layout ll1.


The method is called three times to create three rows of data. A screenshot of the resultant layout is shown below:

Android XML pullparser screenshot

After this tutorial your XmlParserActvity.java file should look similar to the one below:

Download Download tutorial set 2

Privacy and Cookies Disclaimer Copyright
© 2015 - 2018 North Border Tech Training All rights reserved