Dynamically Table Row Creation
In This Tutorial , it will show you how you can create the rows dynamically. Atfer Every click on the button, it will add rows dynamically.
MainActivity.java
public class MainActivity extends Activity implements OnClickListener{
Button btn;
int click=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public void onClick(View v) {
TableLayout tl = (TableLayout)findViewById(R.id.mytable);
TableRow tr = new TableRow(this);
TextView tv= new TextView(this);
click++;
tv.setText("Created New Row: "+"Text:"+click);
tr.addView(tv);
tl.addView(tr,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#eeeeee" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click To Add New Row Dynamically">
</Button>
<ScrollView
android:id="@+id/ScrollView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableLayout
android:id="@+id/mytable"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="0">
</TableLayout>
</ScrollView>
</LinearLayout>
Result:
hgggggggggggggggggggggggggggg
ReplyDelete