[TinyTut] - Dynamically add rows to TableLayout

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

[TinyTut] - Dynamically add rows to TableLayout

Postby plusminus » Sat Jan 26, 2008 10:54 am

[align=center][TinyTut] - Dynamically add rows to TableLayout[/align]

What you learn: You will learn how to add Rows/Lines to a TableLayout during Runtime of your application.

Difficulty: 1.5 of 5 :)

:idea: Questions/Problems: Simply post below...

What it will look like:
[align=center]Image[/align]

Description: The following code-snippet enables you to dynamically add rows to a TableLayout:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 this.setContentView(R.layout.main);
  2.  
  3.  
  4.  
  5.                 /* Find Tablelayout defined in main.xml */
  6.  
  7.                 TableLayout tl = (TableLayout)findViewById(R.id.myTableLayout);
  8.  
  9.                         /* Create a new row to be added. */
  10.  
  11.                         TableRow tr = new TableRow(this);
  12.  
  13.                         tr.setLayoutParams(new LayoutParams(
  14.  
  15.                                                 LayoutParams.FILL_PARENT,
  16.  
  17.                                                 LayoutParams.WRAP_CONTENT));
  18.  
  19.                                 /* Create a Button to be the row-content. */
  20.  
  21.                                 Button b = new Button(this);
  22.  
  23.                                 b.setText("Dynamic Button");
  24.  
  25.                                 b.setLayoutParams(new LayoutParams(
  26.  
  27.                                                 LayoutParams.FILL_PARENT,
  28.  
  29.                                                 LayoutParams.WRAP_CONTENT));
  30.  
  31.                                 /* Add Button to row. */
  32.  
  33.                                 tr.addView(b);
  34.  
  35.                 /* Add row to TableLayout. */
  36.  
  37.                 tl.addView(tr,new TableLayout.LayoutParams(
  38.  
  39.                                 LayoutParams.FILL_PARENT,
  40.  
  41.                                 LayoutParams.WRAP_CONTENT));
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


Where my main.xml was:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.         id="@+id/myTableLayout"
  6.  
  7.    android:layout_width="fill_parent"
  8.  
  9.    android:layout_height="fill_parent"
  10.  
  11.    >
  12.  
  13.         <TableRow
  14.  
  15.                 android:layout_width="fill_parent"
  16.  
  17.                 android:layout_height="wrap_content">
  18.  
  19.                
  20.  
  21.                 <Button android:text="Static Button"/>
  22.  
  23.         </TableRow>
  24.  
  25. </TableLayout>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4

[align=center]Thats it :)[/align]
Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

illegal layout params

Postby AndroidBorkCreature » Wed Feb 06, 2008 1:43 pm

hey,
cool tut.

unfortunately, I do not get it to run. I just copied and pasted.

Still, I get a illegal argument exception:
invalid layout params supplied to android.widget.TableRow@400f5345

What did I do wrong?

thx and regards
AndroidBorkCreature
Freshman
Freshman
 
Posts: 5
Joined: Tue Feb 05, 2008 1:56 pm

Postby plusminus » Wed Feb 06, 2008 6:06 pm

Hello AndroidBorkCreature,

could be because of the a wrong import. I should have posted them to.

Local Eclipse History says I had these:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import android.app.Activity;
  2.  
  3. import android.os.Bundle;
  4.  
  5. import android.widget.Button;
  6.  
  7. import android.widget.TableLayout;
  8.  
  9. import android.widget.TableRow;
  10.  
  11. import android.widget.TableRow.LayoutParams;
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Give it a try... and let us know.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby AndroidBorkCreature » Fri Feb 08, 2008 4:20 pm

Hi,
thanks for the hint. My imports seem to be correct, but if I comment

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  // tr.setLayoutParams(new LayoutParams(
  2.  
  3.                         //      LayoutParams.FILL_PARENT,
  4.  
  5.                         //      LayoutParams.WRAP_CONTENT));
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


it works.

Bye!
AndroidBorkCreature
Freshman
Freshman
 
Posts: 5
Joined: Tue Feb 05, 2008 1:56 pm

Doesnt Work

Postby C3PODroid » Tue Feb 19, 2008 5:36 am

Hi,

I cannot get this code to work either! I even tried commenting out lines as mentioned in the above post but still no luck.

Regards

c3po
C3PODroid
Freshman
Freshman
 
Posts: 2
Joined: Tue Feb 19, 2008 5:33 am

Re: Doesnt Work

Postby Katharnavas » Tue Feb 19, 2008 5:47 am

C3PODroid wrote:Hi,

I cannot get this code to work either! I even tried commenting out lines as mentioned in the above post but still no luck.

Regards

c3po


Are you trying this in latest M5 sdk version if so in the manifest try changing id to android:id
Katharnavas
Senior Developer
Senior Developer
 
Posts: 100
Joined: Tue Dec 04, 2007 5:57 am
Location: India

Top

Re:

Postby C3PODroid » Fri Feb 22, 2008 1:22 am

Yes, I was using M5. Thank you for your help. I should have read the porting documents as most of these tutorials are for M3. Thanks again
C3PODroid
Freshman
Freshman
 
Posts: 2
Joined: Tue Feb 19, 2008 5:33 am

Re: Doesnt Work

Postby seanc » Fri Mar 21, 2008 9:51 pm

Katharnavas wrote:
C3PODroid wrote:Hi,

I cannot get this code to work either! I even tried commenting out lines as mentioned in the above post but still no luck.

Regards

c3po


Are you trying this in latest M5 sdk version if so in the manifest try changing id to android:id


I made this change but I still get an Illegal argument exception. Not sure what I'm doing wrong.
seanc
Freshman
Freshman
 
Posts: 4
Joined: Fri Mar 21, 2008 9:43 pm

What if we add twice the button

Postby xirgonium » Wed Apr 16, 2008 1:05 pm

Actually I tried to add twice the button and the result was amazing :
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. tr.addView(b);
  2.  
  3.              tr.addView(b);
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


I was expecting to have :

+--------------------+
| Dynamic Button |
+--------------------+
+--------------------+
| Dynamic Button |
+--------------------+

But the result is other. See the joined image.

It seems that there is like a new column. The result of dynamcis remains the same even If I remove the static button.

Can someone help me in getting what I expect ? Two buttons the one above the other ?

Thanks !
Attachments
CaptureEcran_01 2008-04-16 14.01.gif
(24.88 KiB) Downloaded 1038 times
xirgonium
Junior Developer
Junior Developer
 
Posts: 10
Joined: Thu Apr 10, 2008 6:17 pm

Postby sxjthefirst » Wed Apr 16, 2008 3:06 pm

Create another TableRow and you can have the buttons one below the other. Like this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 TableRow tr = new TableRow(this);
  2.  
  3.                 tr.setOrientation(TableRow.VERTICAL);
  4.  
  5.                 Button b = new Button(this);
  6.  
  7.                 b.setText("Dynamic Button");
  8.  
  9.                 tr.addView(b);
  10.  
  11.                 TableRow tr1 = new TableRow(this);
  12.  
  13.        
  14.  
  15.                 tr1.addView(b);
  16.  
  17.                
  18.  
  19.                 /* Add row to TableLayout. */
  20.  
  21.                 tl.addView(tr,new TableLayout.LayoutParams(
  22.  
  23.                                 TableRow.LayoutParams.FILL_PARENT,
  24.  
  25.                                 TableRow.LayoutParams.WRAP_CONTENT));
  26.  
  27.                 tl.addView(tr1,new TableLayout.LayoutParams(
  28.  
  29.                                 TableRow.LayoutParams.FILL_PARENT,
  30.  
  31.                                 TableRow.LayoutParams.WRAP_CONTENT));
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
sxjthefirst
Developer
Developer
 
Posts: 36
Joined: Wed Mar 12, 2008 7:24 pm

OK

Postby xirgonium » Wed Apr 16, 2008 4:15 pm

yes it's works perfectly.

Actually I had quite the same behavior (empty rows) by adding rows but I just realize that I need to instanciate new row objects, thanks to your code.

I was expecting that if I had twice the same row, then I will have it twice, but it is not the case.

Thanks a lot, sxthefirst ,for your help. It was really helpfull for me.
xirgonium
Junior Developer
Junior Developer
 
Posts: 10
Joined: Thu Apr 10, 2008 6:17 pm

Postby brijeshthakur » Tue Nov 11, 2008 1:55 pm

Hi Plusminus,

How we can set Span of any column of a Table...I mean How we can achieve this Programatically i.e. Runtime.

I want to create a Dynamic Table with ColSpan.



Thanks in Advance
brijeshthakur
Junior Developer
Junior Developer
 
Posts: 12
Joined: Wed Jul 23, 2008 12:36 pm
Location: Banglore,India

Postby kuixotiq » Sun Apr 12, 2009 9:38 am

So once i create a bunch of dynamic buttons, how do i listen to their actions? cause usually buttons come with an id right? if they're created dynamically, how do i use setonclicklistener, or is there another way? how do i know which buttons's clicked and how can i access that button, etc...
Can someone give me some example code, please

thanks~Kuix
kuixotiq
Once Poster
Once Poster
 
Posts: 1
Joined: Sun Apr 12, 2009 8:48 am

Postby alpesh_harsoda » Wed Apr 29, 2009 2:08 pm

i have done this for that just create one class extends Tablerow and add the components in tablerow.and then add that class object to your main table.
i think this will help you.
Regards,
Alpesh Patel,
Logistic Infotech
http://www.logisticinfotech.com
User avatar
alpesh_harsoda
Senior Developer
Senior Developer
 
Posts: 106
Joined: Sat Sep 20, 2008 12:35 pm
Location: Rajkot,Gujarat(India)

Postby ioRek » Tue Jun 23, 2009 12:45 pm

AndroidBorkCreature wrote:Hi,
thanks for the hint. My imports seem to be correct, but if I comment

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  // tr.setLayoutParams(new LayoutParams(
  2.                         //      LayoutParams.FILL_PARENT,
  3.                         //      LayoutParams.WRAP_CONTENT));
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


it works.

Bye!


it still doesnt work for me :(
ioRek
Freshman
Freshman
 
Posts: 9
Joined: Mon Jun 22, 2009 3:10 pm

Top
Next

Return to Novice Tutorials

Who is online

Users browsing this forum: Yahoo [Bot] and 4 guests