1

I'm working on a big project and I can not solve the problem of a dynamically adding of components.I want to add layout into other layout by click on a button ADD. After this I want to remove it by click a button REMOVE.
Specially for stackoverflow I build a small example of what I want to do. To ADD it's not a problem but remove it's a problem.When I click a "remove" button this remove not what I need (I want remove parent of "remove" button).
After this I want to ask something more important.I will need save all this data to the DB.So I don't know how to get data from each Text Fields and put it into list (or something else) because all this Text Fields have same ID. So I see two way of solution: 1)Change there ID dynamically
2)Something else))
Thank you very much!!!

This is

sub_fields.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:nestedScrollingEnabled="false"
android:id="@+id/detailsLayout"
android:focusableInTouchMode="true">




    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/txtName"
        android:hint="name" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="phone"
        android:ems="10"
        android:id="@+id/txtPhone"
        android:layout_gravity="center_horizontal"
        android:hint="phone" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ADD"
            android:id="@+id/btnAddd"
            android:onClick="onClickAddd" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="REMOVE"
            android:id="@+id/btnRemove"
            android:onClick="onClickAddd" />

    </LinearLayout>
</LinearLayout>

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:nestedScrollingEnabled="false"
android:id="@+id/generalLayout">

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="56dp"
    android:gravity="center"
    android:background="#7d65258a">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="FILL FIELDS"
        android:id="@+id/textView" />
</LinearLayout>

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/subLayoutFields">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/txtName"
        android:hint="name" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="phone"
        android:ems="10"
        android:id="@+id/txtPhone"
        android:layout_gravity="center_horizontal"
        android:hint="phone" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ADD"
            android:id="@+id/btnAdd"
            android:onClick="onClickAdd" />

    </LinearLayout>
</LinearLayout>

MainActivity.java

package andrey.adddinamicallycontrolsapp;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
       getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
       if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    public void onClickAdd(View view) {
        LayoutInflater ltInflater = getLayoutInflater();
       final LinearLayout subLayoutFields = (LinearLayout) findViewById(R.id.subLayoutFields);
       final View view1 = ltInflater.inflate(R.layout.sub_fields, subLayoutFields, true);
        Button buttonRemove = (Button)view1.findViewById(R.id.btnRemove);

        buttonRemove.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {

                subLayoutFields.removeView((LinearLayout)(v.getParent().getParent()));
            }});

    }
Andrey
  • 11
  • 1
  • 4

3 Answers3

0

Before adding some view by addView(childView) you should save reference to that. In that case removeView(childView) will work. Getting that view by traveling in view tree by getParent() is not a good idea.

By the way, where is your addView call?

Ircover
  • 2,406
  • 2
  • 22
  • 42
  • 1
    `ltInflater.inflate(R.layout.sub_fields, subLayoutFields, true);` - last flag `true` means auto-adding this view to parent (`subLayoutFields` in this case) – snachmsm Apr 16 '15 at 11:47
  • Ok, then do you want remove `view1`? – Ircover Apr 16 '15 at 11:51
  • I don't know how to save reference to views that I add because all the views have the same ID.I think that is why I can't remove it. – Andrey Apr 16 '15 at 12:22
  • You can set ID for your inflated view by calling `view1.setId(id)`. – Ircover Apr 16 '15 at 12:27
0

maybe try subLayoutFields.removeView(view1) - view1 is final

if you want to somehow differ your widgets (for storing in db, iterating or whatever you want) you may set for them dynamic ids using View.generateViewId() (API17) or using custom method from most voted answer here

Community
  • 1
  • 1
snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • I think setting the Id of the view is the right approach. – AlvaroSantisteban Apr 16 '15 at 11:51
  • nothing at all? have you tried logging or showing `Toast` inside `onClick`? you may need to use `subLayoutFields.requestLayout();` or `subLayoutFields.invalidate();` but I think that `removeView` call one of these methods also after removing – snachmsm Apr 16 '15 at 12:46
0

Thank you guys!!! The answer is :

public void onClickAddPanel(View view) {

            LayoutInflater ltInflater = getLayoutInflater();
            LinearLayout subLayoutFieldsForBtnAdd = (LinearLayout) findViewById(R.id.productDetails);
            View view1 = ltInflater.inflate(R.layout.product_details, subLayoutFieldsForBtnAdd, true);

}


public void onClickRemovePanel(View v) {
    View v1 = (View) v.getParent();
    LinearLayout subLayoutFieldsForBtnRemove = (LinearLayout) findViewById(R.id.productDetails);
    subLayoutFieldsForBtnRemove.removeView(v1);
}
Andrey
  • 11
  • 1
  • 4
  • This code is also not working for me. App gets crashed on click remove button. It shows exception like :- Could not find method onClickAddd(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'btnRemove' – Priya Jagtap Oct 05 '16 at 07:07