In my quest to create a “Taxman” Android application I ran into the following problem:
I want to ask the user to enter their yearly income, after which the screen will flip and their after-tax income will be displayed in a table format for all Canadian provinces. (And for the future: all US states). The problem I ran into is creating a TableRow for each Canadian province in my main.xml. There are 13 provinces and territories, so 13 is not that bad to copy & paste in the main.xml; however, as soon as I have to copy & paste more than 2 times I start cringing and nausea kicks in. Personally I think “copy & paste” should not be in the programming vocabulary. Not only that, but I have 13 provinces now, what about when I have to enter 50 states! So, I had to find a way to create these new TableRow rows programmatically inside my TableLayout layout.
A great source for code snippets and answers to Android questions is http://www.anddev.org. I’m sure if you’re dabbling with Android then you’ve already come across this site. The particular post I read to do this is this one: Dynamically add rows to TableLayout. Commenters on that post were having problems getting this to work, but after I imported all the correct classes (android.widget.TableRow.LayoutParams is an important one to use) everything worked from the first time. I forced Eclipse to find most of the classes for me.
Below is the code from anddev.org, with my changes for my Taxman Android app. I’m too lazy to do a full-out beginning-to-end tutorial for this code snippet.
First I set my constants at the top of the Activity class:
public class Activity1 extends Activity implements OnTouchListener{ int PROVINCE_Alberta = 0; int PROVINCE_BC = 1; int PROVINCE_Manitoba = 2; int PROVINCE_NewBrunswick = 3; int PROVINCE_Newfoundland = 4; int PROVINCE_Northwest = 5; int PROVINCE_NovaScotia = 6; int PROVINCE_Nunavut = 7; int PROVINCE_Ontario = 8; int PROVINCE_PEI = 9; int PROVINCE_Quebec = 10; int PROVINCE_Saskatchewan = 11; int PROVINCE_Yukon = 12; int numProvinces = 13;
There are 13 of them this time, there will be 50 of them for the States… technically this could be made into something much more malleable like an external config-type XML file.
Then in the onCreate method I created an array of String objects that will house all these provinces:
String[] provinces = new String[numProvinces]; provinces[PROVINCE_Alberta] = "Alberta"; provinces[PROVINCE_BC] = "British Columbia"; provinces[PROVINCE_Manitoba] = "Manitoba"; provinces[PROVINCE_NewBrunswick] = "New Brunswick"; provinces[PROVINCE_Newfoundland] = "Newfoundland and Labrador"; provinces[PROVINCE_Northwest] = "Northwest Territories"; provinces[PROVINCE_NovaScotia] = "Nova Scotia"; provinces[PROVINCE_Nunavut] = "Nunavut"; provinces[PROVINCE_Ontario] = "Ontario"; provinces[PROVINCE_PEI] = "Prince Edward Island"; provinces[PROVINCE_Quebec] = "Quebec"; provinces[PROVINCE_Saskatchewan] = "Saskatchewan"; provinces[PROVINCE_Yukon] = "Yukon";
And right underneath that I create a TableRow for each item in the array provinces:
// Get the TableLayout TableLayout tl = (TableLayout) findViewById(R.id.maintable); // Go through each item in the array for (int current = 0; current < numProvinces; current++) { // Create a TableRow and give it an ID TableRow tr = new TableRow(this); tr.setId(100+current); tr.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); // Create a TextView to house the name of the province TextView labelTV = new TextView(this); labelTV.setId(200+current); labelTV.setText(provinces[current]); labelTV.setTextColor(Color.BLACK); labelTV.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); tr.addView(labelTV); // Create a TextView to house the value of the after-tax income TextView valueTV = new TextView(this); valueTV.setId(current); valueTV.setText("$0"); valueTV.setTextColor(Color.BLACK); valueTV.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); tr.addView(valueTV); // Add the TableRow to the TableLayout tl.addView(tr, new TableLayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); }
The main.xml contains the following for the TableLayout:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:stretchColumns="0,1" android:id="@+id/maintable" > </TableLayout>
And here is the final result:
So far I have the Ontario taxes working :). If you make $50,000 in Ontario, you’d be taking home $38,285.53.

September 8, 2009 at 08:58
This is close to what I am looking for!
I have the table building dynamically, called from onCreate() … my problem is that the table shows MONTH based figures with a Spinner above it. You can choose a new month from the Spinner and the table ‘is supposed to’ update the the selected month’s data.
If you know… could you point me in the direction to be able to do this? I don’t under stand how to clear the table and draw a new one programatically in it’s place each time the month is changed…
Obviously, I’m a little new to Android. I’m a little disappointed at how difficult tables have been so far. I eventually want to populate the table from a sql query result set, but I’ll get to that later once the testing works!
Thanks!
Steve