Android: Creating TableRow rows inside a TableLayout programatically

Posted July 1st @ 10:07 pm by Boyan Tsolov


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 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, 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(;

        // 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.setLayoutParams(new LayoutParams(

            // Create a TextView to house the name of the province
            TextView labelTV = new TextView(this);
            labelTV.setLayoutParams(new LayoutParams(

            // Create a TextView to house the value of the after-tax income
            TextView valueTV = new TextView(this);
            valueTV.setLayoutParams(new LayoutParams(

            // Add the TableRow to the TableLayout
            tl.addView(tr, new TableLayout.LayoutParams(

The main.xml contains the following for the TableLayout:

        <TableLayout xmlns:android=""
                android:id="@+id/maintable" >

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.

4 Trackbacks/Pingbacks

  1. Pingback: Android: Creating TableRow rows inside a TableLayout programatically - android xml ???? on December 15, 2010
  2. Pingback: Best way to present tabular data? on October 30, 2012
  3. Trackback: australian international school on January 30, 2015
  4. Trackback: professional best free website design software reviews on March 22, 2015


  1. Steve Glachan
    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!



  2. Paresh Mayani
    March 29, 2010 at 07:41

    i have tried this in my code for the demo of dynamically adding row to a tablelayout as such it may be very much helpful in my project.

    But i cant find anything on screen when i run it.

    pls suggest and make me out……

    Thanx in advance

  3. macaufuns
    May 26, 2010 at 05:14

    Paresh Mayani,
    I have the same problem, and then I remove “valueTV.setLayoutParams(new LayoutParams(
    ” from each textview, something is displayed.

    Try it!

  4. pula
    July 2, 2010 at 22:14


    that works for me too! I wonder why?

  5. Anond
    July 13, 2010 at 10:53

    I’ve got a force close when I tried to add the values to the table layout.. Anyone know why ? I’vent changed any part of the code.

  6. Anond
    July 13, 2010 at 10:58

    Nevermind… I I’ve put the wrong xml on the contentView

  7. Anond
    July 13, 2010 at 10:58

    Thanks by the way, you’ve helped me a lot

  8. LaszloLaszlo
    August 21, 2010 at 04:09

    macaufuns: I had the same problem when i imported a wrong LayoutParams class. Try importing android.widget.TableRow.LayoutParams

    br, Laszlo

  9. Kabilan
    December 31, 2010 at 02:55

    Good Tutorial !

    Initially I got plain black screen ie., nothing.

    Found out at last. Text color has been set to black and background colour hasnt been set.


    I think you can see it now : )

  10. J-ing
    January 7, 2011 at 00:15

    Hi, is it possible to hv this at the bottom?

    public boolean onTouch(View arg0, MotionEvent arg1) {
    // TODO Auto-generated method stub

    return false;

    cos if i wan to make it to be able to scroll, is there anything that i need to add on?

  11. rahul
    February 9, 2011 at 01:29

    Hi basically we dont need to set layout for textView controls in the above example .

    Just remove that one ant tried it works fine .

  12. Krishna prasad
    February 24, 2011 at 02:35

    Hi,In my project also i used same logic, but i have a lot of data(content) to show in the form of table it takes too much time to load content and show in the page.please send suggestion to my mail how to improve my performance.
    thank u.

  13. Zack
    March 10, 2011 at 17:02

    This is a great post. However, is it not possible to just create a table_row.xml that defines a single TableRow, and then inflate that and just populate it, as opposed to having to write out all the attributes in Java?

  14. Dhara
    April 8, 2011 at 04:24

    I use above code for tablelayout I got force close when run the application. I also debug code it go in catch block from sentence table.addView(tableRow);
    Help me. Thanks in advance.

  15. Vidhi
    October 3, 2011 at 04:51

    In regards to the solution provided for the table row to show up (i.e. by removing the setLayoutParams for the TextView), how would i do it, if i have a long line of text.
    I tried removing the text wrapping, but then i lose part of my text too. Can anyone help me with this?
    For my entire text to show up, i wud need layoutparams to be wrap_content for my TextView

  16. dunghv
    October 10, 2011 at 19:34

    hi, i try your example on android 3.0, but i don’t textvie on screen. when, i comment code line:
    labelTV.setLayoutParams(new LayoutParams(
    valueTV.setLayoutParams(new LayoutParams(

    textviews is show, how do use textview.setLayoutParams
    thanks help me!

  17. muzaffer
    June 5, 2012 at 10:20

    Dhara! I was having same problem, too. Do not forget to initialize tablelayout by tl = (TableLayout) findviewbyid(…

  18. Douglas
    August 2, 2012 at 13:41

    Awesome buddy! Thanks… You rock!

  19. kirit
    December 30, 2013 at 07:50

    Really very helpful

  20. Tarun
    July 31, 2014 at 06:03

    Here is a link i have taken help from it to use table layout Adding DYNAMIC TABLE ROWS IN TABLELAYOUT PROGRAMICALLY in android . It also contains files to download code

  21. adidas ?????
    August 4, 2014 at 04:50

    Rip DVD Movies to iTunes iPhone, iPad, iPod all deliver the same message and it is to stay connected to music and videos nonstop all the days of their life as long as the battery is in the device. It is also a very remarkable statistic that most youngsters of today are the people who are so glued into the trend of plugging something into their ears and hearing it all the time. What one must realize is that these are not the only devices that are in the portable entertainment market today, but there are a host of other accompanied which have glutted this industry with so many models leaving a very confused consumer unless he has done his homework and is quite certain about this requirements and tastes. Most often it is not taste that hinders but the warmth of one’s pocket that matters more. The advent of iTunes mp4 This is one of the most versatile container formats of this century and it owes its popularity to the fact that almost all devices, is they phones or other portable entertainment devices, is all compatible with this format. This mp4 is able to contain a wide variety of files and it is this fact that makes it such a adaptable and user friendly format with people. For example, one may download a movie in the mp4 format and then play a wide variety of subtitles in various languages all in addition to this and have a picture of the DVD cover on it as well. All these can be succinctly contained within a single file and this is the true genius of the mp4 format. All these require that any DVD which has been released be converted to an mp4 format so that it can be watched on the go anywhere and at any time with the ease of lounging from a bus chair or plopped up with pillows on a train. Conversion of files In order for a person to view these on portable devices, one has to ensure that his DVD is converted into the mp4 format as well and this is where Aimersoft’s software products come of use. This product is great to rip DVD iTunes without any hassle, since it comes with an inbuilt tutorial on how to convert DVD to iTunes and all the required steps are enumerated very lucidly so that one can complete his task with minimum amount of fuss and flutter. There are many dvd ripper iTunes available to a person and therefore it is wise to read a few reviews before stepping off ripping DVD to iTunes. This is an important step, simply because many people have stored movies on their DVDs but would like them to be out in the open on their portable devices, so that they can watch the movie in their free time. This being the strongest reason possible, one must consider the usage of this software so that he can complete his conversion quickly and watch his movie on the move. copy DVD to iTunes rip DVD into iTunes

Leave a comment

Standard Login



  • spirtual black art pictures
  • bar girls sex movies
  • thirty degrees petite sirah
  • free hairy fat pics
  • Big boobs in saree
  • amateur loose pool slut wife videos
  • pharmacy lab coats petite
  • sex arabic video free
  • oiled ass video
  • old and young lesbian video
  • free small tits sex videos
  • girlfriend is making me her bitch
  • Nude pics of miss usa
  • courtney foxxx ebony
  • ebony virgin taking black cock
  • best free streaming ebony
  • free amateur wife blowjob videos
  • mennonite sex pics
  • young tiny hardcore vids
  • little lolta girls pussy
  • amateur teens beach
  • trimmed pussy fuck
  • Sexy kerala girls
  • girls jacking boys off fuck lesbian
  • black magic woman movie
  • amateur black ladies nude
  • hot babes soft video
  • sex pictures black woman
  • sexy girls threesome videos
  • xhamster older wpman seducing young girl
  • teen girls screwing baby brothers
  • Teen fucked old
  • hot young emo girls naked
  • dys littlt girl pussy
  • amy reid facial movies
  • blond japanese girl
  • videos of girls humping pillow
  • married couple hardcore photo gallery
  • Really young hot girls
  • sexy asian wamen
  • free anal avi
  • homade sex tube wife swinger
  • sexy secretary ass
  • phat butts free videos
  • free stocking upskirt pussy thumbs
  • free mmmf sex pics
  • sexy women ass tits
  • party girls caught pissing
  • girls xxx pic
  • brett farve naked video
  • office blowjob videos
  • hot sex amateur
  • Teen nudists pageant
  • xxx mmf ffm
  • sexy picutres of girls
  • Jayne mansfield nude pictures
  • ature hairy pussy
  • very young bikini
  • what is butt sex
  • male sex partners for couples minneapolis
  • christine agulera pussy
  • first time anal sex instructions
  • xxx free anal
  • lactation free wife fuck
  • Beautiful indian tits
  • alyssa miliano leaked sex video
  • Black sex teen
  • teens with hairy pussy
  • free tiny teens reality nude pics
  • highschool musiacl girl naked
  • Girls with shaved pussies
  • tori black full length blowjob
  • dailymotion topless girlfriend
  • puritans sex videos
  • free sex movies in bath rooms
  • petite executive chair
  • ebony amatuers free
  • babes over gallery
  • bachloret party girls
  • xxx wife craves black
  • young babes pussy
  • sexy young girl amateur
  • asian mixed black men
  • free hot teen force video
  • mama sweet pussy young dicks
  • free streaming movies grinding lesbians
  • young little petite asian
  • sex video mpeg
  • Hot bed room sex
  • choking whore bitch video xxx
  • wife bra pics
  • girl sucking shemale dicks
  • hairy pussy young pics
  • mature big cock movies
  • black on white xx sex
  • young latina girl
  • crossdresser free vids
  • kinky teen petite young girl
  • lick cum hairy pussy
  • mature women webcams live
  • free pics of cartoon wifes naked
  • Sex mother hot
  • video of black girls lesbian
  • old men with young girls
  • ebony on white dicks
  • julia video sex
  • xxx hairy black pussy movies
  • beach party hardcore
  • teen young model video bj
  • redtube teen girls kissing
  • pictures big nipples
  • asian girls next door
  • sex dick topless butt
  • free fuck tits videos
  • round ass girls
  • movies hairy mature women
  • interracial swingers sex pics
  • free of saved pussy
  • erotic pictures young girl
  • asian teens deflorated
  • tube movies ebony
  • Black on white sex
  • black anal thumbs
  • pictures book lick me cherry vanilla
  • free american movies
  • webcam girls dorie
  • oz mature pussy
  • topless sailing video
  • deep anal lesbians
  • horny moms want young dick
  • hot sexy teen toying with bananaz
  • virgin latina pussy
  • mature teaches teen to suck dick
  • men on young girls free
  • sex videos nude
  • wife has a threesome
  • sexy girls ass and pussy
  • blowjob facial video
  • Girls getting fucked good
  • couples sex massage
  • tiffany price facial
  • jesse jane free hardcore movie
  • solo teen girls pocahontas hotfile
  • angelena jolee video naked nude
  • top tits movies
  • emma watson naked picnic pictures
  • pissing pussy teen
  • movie young people fucking trailer
  • sexiest ebony women
  • shaved pissing pussy free videos
  • ukraine nonnude girls young
  • Sexy girls with sexy legs
  • young asian wet pussy
  • girl spreading open ass for pics
  • homemade titty fuck compilation video
  • mature women pussy vidoes
  • free very young teen sex movies
  • Teen gets creampie
  • petite asian ladyboy
  • egyptian galleries
  • plantation wife fucked by black slave
  • things lost in pussy
  • cartoon sex animation pictures
  • petite lenox window
  • japanese mpeg
  • video readhead naked girls
  • hustler girls party
  • Soccer girls nude
  • videos naked around kids
  • nazi sex experiments movie
  • mature pussy next door
  • Young naked chinese girls
  • free amateur videos of outdoor masturbation
  • shemale sexchange tube
  • hot young black boys fuck
  • san juan pussy
  • blonde shaves pussy
  • Rikki and vikki nude pics
  • black teen n free movies
  • anal sex cause stomach problems
  • cinco de mayo sex videos
  • Boob cum shots
  • free with sex teens
  • seky asian girls
  • amateur video man and woman
  • Naked latinas girls
  • young beauty nude girls
  • cute tween girls pics
  • drunk hot girls sex vudeo
  • sex videos x hub
  • teenagers surgically turned into girls
  • sexy legs and tight ass
  • Namitha sex videos
  • free ebony video thumbnails
  • code lyoko pictures galleries
  • girls in bikini taking them off
  • The reader sex scenes video
  • free hairy teen gallery
  • sophia lomeli movies
  • tips on fingering girl
  • teen babe with hairy pussy
  • brown eyed girl anal
  • Ipod sex movie
  • hot girl takes cock in ass
  • free interracial sex videos
  • misty nasty wife black bachelor
  • blackberry messenger sex group adult
  • free czech girls
  • pussyfucking cam asian young
  • real sister brother sex videos
  • teen girl fucked in woods
  • sexy gorgeous girls
  • webcam nude couples
  • jenna haze huge black anal
  • little pre pussy
  • older senior pussy
  • Petite naked girl
  • xxx cute young girls
  • sweet nude boobs pics of babes
  • best full length streaming sex videos
  • teenie black hood ass
  • hentia pics
  • raven black sexy couple
  • murdered beauty contest girl
  • girl wants to fuck pet
  • nasty ebony anal
  • Lacey duvalle nude videos
  • sexy naked wives and girlfriends
  • boy eats girl movie trailer
  • tv sitcom pussy
  • white butt pictures
  • free girl fisting videos
  • dick rub ass cheeks
  • Sex scenes from movies
  • old nude mature gallery
  • klixen blowjob free video
  • free indian pussy videos
  • tiny erotic teens
  • short video blowjob
  • bbw free full lenght sex videos
  • nude swedish emo teen movies
  • hien cameras ch naked girls
  • girls suck big dicks
  • playboy mansion girls boobs being touched
  • girl showing pussy in store
  • hot girls getting sex
  • young teen homemade fuck movies
  • hairy pussy flash movies
  • girls taking big cock amateur
  • just the little girls pussy
  • pictures of rimming during sex
  • Portuguese sexy girls
  • little boy girl sex
  • polish naked girl
  • hard erotic massage free video
  • fat gilrs pussy