Friday, 8 February 2013

Creating a dynamic row in TableLayout..


All of you know to add a no.of rows in Table-layout by using XML.but here i will provide you the
guideline for generating rows dynamically in your android code.


Step:1)First of all you add Table-layout in your XML file.like following

         <TableLayout
                       android:id="@+id/tbl_product"
                       android:layout_width="fill_parent"
                      android:layout_height="wrap_content" >

        </TableLayout>


Step:2)Then u add the follwing code in your java file.


A) First,you set up the control for table layout.

         TableLayout prodlist = (Table Layout) findViewById(R.id.tbl_product);


B) Second,you will create a method name display(). in that u create a row using follwing:

        void display(String id, String name) {

        Table Row tr2 = new Table Row(this);
        tr2.layout(0, 0, 0, 0);


C) Third,you will create a two text view which will be added in that above row and set the argument value of display() method to that text view.


First text-view:


      TextView lblid = new TextView(this);
        lblid.setText("" + id);
        lblid.setTextColor(Color.BLACK);
        lblid.setTextSize((float) 11.5);
        lblid.setTypeface(null, 1);
        lblid.setBackgroundColor(Color.TRANSPARENT);
        lblid.setPadding(30, 0, 0, 0);


        tr2.setPadding(0, 1, 0, 0);
        tr2.addView(lblid);


Second text-view:


      TextView lblname = new TextView(this);
        lblname.setText("" + name);
        lblname.setTextColor(Color.BLACK);
        lblname.setTextSize((float) 11.5);
        lblname.setTypeface(null, 1);
        lblname.setBackgroundColor(Color.TRANSPARENT);
        lblname.setPadding(30, 0, 20, 0);
        tr2.setPadding(0, 1, 0, 1);
        tr2.addView(lblname);

 

Step:3)

Next most important thing is add the table row to the tableLayout by the following:


       prodlist.addView(tr2, new TableLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));



Thanks...

4 comments:

  1. The following Link may also Help:
    http://include.site40.net/Androidtable.php

    ReplyDelete
  2. hi it's good but how to add the borders for that table

    ReplyDelete
  3. @sai:-Refer this http://uniqueandroidtutorials.blogspot.in/2013/10/android-to-draw-line-and-rounded-corner.html..and add this round corner xml to the background of the table so its displaying a border to the table.

    ReplyDelete

 

Copyright © Unique Android Tutorial.. Design by Free CSS Templates | Blogger Theme by BTDesigner | Powered by Blogger