Delete row dynamically from table layout in android
-
14-06-2021 - |
Question
I am having a TableLayout
for which I added rows dynamically. In each of the row there are 2 elements of which one is TextView
other is Button
. When I click the button that is present in a row, that row should be deleted. How can this be done in Android ? How to find rowid and how to delete a row dynamically. Can anyone help me in sorting out this issue.
Solution
onClick on the button will give you the clicked view, the button in your case. That button's parent is the row you want to delete. Deleting that row from it's parent will get rid of it.
An example of how you could implement this:
button.setOnClickListener(new OnClickListener()
{
@Override public void onClick(View v)
{
// row is your row, the parent of the clicked button
View row = (View) v.getParent();
// container contains all the rows, you could keep a variable somewhere else to the container which you can refer to here
ViewGroup container = ((ViewGroup)row.getParent());
// delete the row and invalidate your view so it gets redrawn
container.removeView(row);
container.invalidate();
}
});
OTHER TIPS
You need to assign ID for dynamically adding rows and with the use of that you can get the values of that particular row or you can also delete the row which you click the row button.
In onCreate():-
addButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mTable.addView(addRow(mInput.getText().toString()));
}
});
private TableRow addRow(String s) {
TableRow tr = new TableRow(this);
tr.setId(1000 + sCount);
tr.setLayoutParams(new TableLayout.LayoutParams(
TableLayout.LayoutParams.FILL_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));
TableRow.LayoutParams tlparams = new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT);
TextView textView = new TextView(this);
textView.setLayoutParams(tlparams);
textView.setText("New text: " + s);
tr.addView(textView);
TableRow.LayoutParams blparams = new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT);
final Button button = new Button(this);
button.setLayoutParams(blparams);
button.setText(" - ");
button.setId(2000 + sCount);
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
mTable.removeView(findViewById(v.getId() - 1000));
}
});
tr.addView(button);
sCount++;
return tr;
}
TableLayout:-
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TableLayout
android:id="@+id/table1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TableLayout>
</LinearLayout>
</ScrollView>