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

18 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 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
  5. Impressive. Your story always bring hope and new energy. Keep up the good work.
    Blueprism training institute in Chennai

    Blueprism online training

    ReplyDelete
  6. Thanks for splitting your comprehension with us. It’s really useful to me & I hope it helps the people who in need of this vital information. 
    devops online training

    aws online training

    data science with python online training

    data science online training

    rpa online training

    ReplyDelete
  7. Your very own commitment to getting the message throughout came to be rather powerful and have consistently enabled employees just like me to arrive at their desired goals.
    Data science Course Training in Chennai | Data Science Training in Chennai
    RPA Course Training in Chennai | RPA Training in Chennai
    AWS Course Training in Chennai | AWS Training in Chennai

    ReplyDelete
  8. Can you please let me know how to assign ids to all these elements dynamically ? so that i can save the information .

    ReplyDelete
  9. Great! Information you have been shared, it’s really very impressive and easy to understand please share more useful information like this. Thank you
    Data Science Training in Hyderabad

    ReplyDelete

  10. Nice article and thanks for sharing with us. Its very informative


    Plots in CHITUKULA

    ReplyDelete
  11. Grab the Digital Marketing Training in Chennai from Infycle Technologies, the best software training institute, and Placement center in Chennai which is providing professional software courses such as Data Science, Artificial Intelligence, Cyber Security, Big Data, Java, Hadoop, Selenium, Android, and iOS Development, DevOps, Oracle, etc with 100% hands-on practical training. Dial 7502633633 to get more info and a free demo and to grab the certification for having a peak rise in your career.

    ReplyDelete
  12. Some may stag in Interviews!!! OOPS!! More than 50% of students do this in their career. Instead, do Hadoop Training in Chennai at Infycle. Those students can easily clear this Interview session because more than 5 times at INFYCLE practicing mock-interview sessions, Hence students are Getting out of their interview fear.

    ReplyDelete