View

Tuesday, 24 May 2011

How To Add a Row Dynamically

 whenever the button is clicked, it adds a new row to the TableLayout and notice the Scrollbar from the ScrollView.

XML CODE:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:orientation="vertical">

<Button android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click To Add New row"></Button>

<ScrollView android:id="@+id/ScrollView01" android:layout_width="wrap_content" android:layout_height="wrap_content">

  <TableLayout android:id="@+id/TableLayout01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:stretchColumns="0">

    <TableRow android:id="@+id/TableRow01" android:layout_width="wrap_content" android:layout_height="wrap_content">
        <TextView android:layout_width="fill_parent" android:text="textfield 1-1" android:layout_height="wrap_content" android:id="@+id/TextView01"></TextView>
        <ImageView android:id="@+id/imageView1" android:layout_height="wrap_content" android:src="@drawable/icon" android:layout_width="wrap_content"></ImageView>
      <DigitalClock android:text="DigitalClock" android:layout_width="wrap_content" android:id="@+id/digitalClock1" android:layout_height="wrap_content"></DigitalClock>
      <CheckBox android:id="@+id/CheckBox01" android:text="" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox>
    </TableRow>

  </TableLayout>
</ScrollView>
</LinearLayout>

[Sorce code=tablelayout.java]

public class tablelayout extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
Button btn;
int counter=0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn=(Button)findViewById(R.id.Button01);
        btn.setOnClickListener( this);
       
    }
    public void onClick(View view){
    TableLayout tl=(TableLayout)findViewById(R.id.TableLayout01);
    TableRow tr=new TableRow(this);
    counter++;
    TextView tv= new TextView(this);
    tv.setText("text"+counter);
    CheckBox cb=new CheckBox(this);
    DigitalClock dc= new DigitalClock(this);
    ImageView ib=new ImageView(this);
    ib.setImageResource(R.drawable.icon);
    tr.addView(tv);
        tr.addView(ib);
        tr.addView(dc);
        tr.addView(cb);
        tl.addView(tr,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
     
    }
  
}

[/source code]  
Output Like This.


NOTE:TableLayout is built using the TableLayout and the TableRow commands. There is no TableCols like the<td> tag in HTML. To align your view in columns you have to set the width of the elements and manually control the layout.So

13 comments:

  1. I really wana thank you for providing such informative and qualitative material so often.
    google android news

    ReplyDelete
  2. can you tell me how to remove row dinamically? thank you

    ReplyDelete
  3. Add one more Button and name as "Click to Remove".

    remove_btn=(Button)findViewById(R.id.Button02);
    Set remove_btn.setOnClickListener( this);

    //*** Place this code***//
    TableLayout tl=(TableLayout)findViewById(R.id.TableLayout01);
    TableRow tr=new TableRow(this);
    counter--;
    TextView tv= new TextView(this);
    tv.setText("text"+counter);
    CheckBox cb=new CheckBox(this);
    DigitalClock dc= new DigitalClock(this);
    ImageView ib=new ImageView(this);
    ib.setImageResource(R.drawable.icon);
    tr.removeView(tv);
    tr.removeView(ib);
    tr.removeView(dc);
    tr.removeView(cb);
    tl.removeView(tr,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

    ReplyDelete
  4. Thanks for the informative article. This is one of the best resources I have found in quite some time. Nicely written and great info. I really cannot thank you enough for sharing.

    rpa Training in Chennai

    rpa Training in bangalore

    rpa Training in pune

    blueprism Training in Chennai

    blueprism Training in bangalore

    blueprism Training in pune

    rpa online training

    ReplyDelete
  5. Thanks you for sharing this unique useful information content with us. Really awesome work. keep on blogging
    python training in pune
    python online training
    python training in OMR

    ReplyDelete
  6. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
    Devops training in velachery
    Devops training in annanagar
    Devops training in sholinganallur

    ReplyDelete
  7. Impressive. Your story always bring hope and new energy. Keep up the good work.
    Blueprism training institute in Chennai

    Blueprism online training

    ReplyDelete
  8. UiPath Training in Bangalore by myTectra is one the best UiPath Training. myTectra is the market leader in providing Robotic Process Automation on UiPath
    ui path training in bangalore

    ReplyDelete
  9. The knowledge of technology you have been sharing thorough this post is very much helpful to develop new idea. here by i also want to share this.
    angularjs Training in bangalore

    angularjs Training in btm

    angularjs Training in electronic-city

    angularjs online Training

    angularjs Training in marathahalli

    ReplyDelete
  10. Howdy, would you mind letting me know which web host you’re utilizing? I’ve loaded your blog in 3 completely different web browsers, and I must say this blog loads a lot quicker than most. Can you suggest a good internet hosting provider at a reasonable price?
    Amazon Web Services Training in OMR , Chennai | Best AWS Training in OMR,Chennai


    Amazon Web Services Training in Tambaram, Chennai|Best AWS Training in Tambaram, Chennai

    ReplyDelete