Brian
Brian

Reputation: 5028

How to add and remove linear layout by button click

I'm trying to use two buttons to dynamically add and remove linear layout.

    protected void createT () {
    // -----------------------------------------------
    count++;
    LinearLayout temp_ll, frame;
    frame = new LinearLayout(this);
    frame.setOrientation(LinearLayout.VERTICAL);
    frame.setId(count);
    EditText temp1, temp2;

    for (int i=0; i<numClass; i++) {

        temp_ll = new LinearLayout(this);
        temp_ll.setOrientation(LinearLayout.HORIZONTAL);

        temp1 = new EditText(this);
        temp2 = new EditText(this);
        temp2.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        temp1.setHint("class name");
        temp2.setHint("grade");

        temp_ll.addView(temp1);
        temp_ll.addView(temp2);
        frame.addView(temp_ll);
    }

    ll.addView(frame);
}

protected void deleteT() {
    // --------------------------------------
    if (count > 0) {
                    LinearLayout temp = new LinearLyout(this);
        temp = (LinearLayout) findViewById(count);
        temp.removeAllViews();
        temp.setVisibility(View.GONE);
        count--;
    }
}

The problem is that I'm using count variable to keep track of linearLayout IDs.
First time when I press add button x times and then press del button everything is fine.

THE PROBLEM IS THAT AFTER PRESSING ADD SECOND TIME .

Upvotes: 1

Views: 4861

Answers (1)

kameny
kameny

Reputation: 2370

I checked your code and I found a little mistake inside the deleteT() method, you remove all view inside the LinearLayout, but you didn't remove the actual layout from the root layout.

And an another suggestion, you don't need a new LinearLayout instance when you want to use findviewbyid method, and use ll.removeView(View) method to delete the dynamic LinearLayout container.

Here is the code:

private int count;
private LinearLayout ll;
private final int numClass = 1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ll = (LinearLayout) findViewById(R.id.main_linearlayout);
}

public void createT(View view) {
    count++;
    final LinearLayout frame = new LinearLayout(this);
    frame.setOrientation(LinearLayout.VERTICAL);
    frame.setId(count);

    for (int i = 0; i < numClass; i++) {
        final LinearLayout temp_ll = new LinearLayout(this);

        final EditText temp1;
        final EditText temp2;
        temp_ll.setOrientation(LinearLayout.HORIZONTAL);

        temp1 = new EditText(this);
        temp2 = new EditText(this);
        temp2.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        temp1.setHint("class name");
        temp2.setHint("grade");

        temp_ll.addView(temp1);
        temp_ll.addView(temp2);
        frame.addView(temp_ll);
    }
    ll.addView(frame);

}

public void deleteT(View view) {
    if (count > 0) {
        final LinearLayout temp = (LinearLayout) ll.findViewById(count);
        temp.removeAllViews();
        ll.removeView(temp);
        count--;
    }
}

And the layout xml:

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

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="add"
        android:onClick="createT" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="delete"
        android:onClick="deleteT" />

</LinearLayout>

I hope it helps!

Upvotes: 4

Related Questions