I think you may find your answer here: Dynamically adding and removing table rows - Android
Use the methods provided by table layout to get index of your row you want to delete.
Question
I'm new to all this, and I need a little help in this.
Here is my axml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableLayout
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/tableLayout1">
<Button
android:text="Add"
android:id="@+id/button1" />
<TableRow
android:id="@+id/tableRow1"
android:layout_marginTop="10dp">
<TextView
android:text="First Name"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_column="0"
android:id="@+id/textView12" />
<TextView
android:text="Second Name"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_column="1"
android:id="@+id/textView13"
android:layout_marginLeft="10dp" />
<TextView
android:text="Colleagues"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_column="2"
android:id="@+id/textView14"
android:layout_marginLeft="10dp" />
<ImageView
android:src="@android:drawable/ic_menu_gallery"
android:layout_column="3"
android:id="@+id/imageView1"
android:layout_marginLeft="10dp" />
<Button
android:text="Delete"
android:id="@+id/button1"
android:layout_marginLeft="10dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginBottom="0.0dp" />
</TableRow>
</TableLayout>
And here is my activity:
public class Activity1 : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
TableLayout t1 =(TableLayout) FindViewById<TableLayout>(Resource.Id.tableLayout1);
Button add = FindViewById<Button>(Resource.Id.button1); //this is just a test for the add button
add.Click += delegate
{
TableRow tR = new TableRow(this);
TextView tV_txt1 = new TextView(this);
TextView tV_txt2 = new TextView(this);
tV_txt1.Text = "A new row";
tR.SetPadding(0, 0, 0, 0);
tV_txt2.Text = "Another new row ";
tR.AddView(tV_txt1);
tR.AddView(tV_txt2);
t1.AddView(tR);
};
}
}
}
The problem is, that I can't find a way to delete from the button the row, the delete button is, of course without affecting the other rows. I would gladly appreciate some help.
Solution
I think you may find your answer here: Dynamically adding and removing table rows - Android
Use the methods provided by table layout to get index of your row you want to delete.
OTHER TIPS
Not sure if this is what you're looking for exactly. You can consider setting the visibility of the Button
to gone, like this.
button.setVisibility(View.GONE);
This will effectively removes the button but allows you bring it back by setting the visibility to View.VISIBLE
in the future if you wish.