0

I got The specified child already has a parent. You must call removeView() on the child's parent first. ERROR.

But I do not want to remove the parent view.

If you click a button, a message must be added at the end of LinearLayout's childViews.

This is my Java code:

    Button addBTN = findViewById(R.id.addButton);
    LinearLayout messageListLL = findViewById(R.id.messageList_LinearLayout);
    addBTN.setOnClickListener((v) -> {
        MessageItem messageItem = new MessageItem();
        View messageView = View.inflate(this, R.layout.item_message, messageListLL);
        messageListLL.addView(messageView, messageListLL.getChildCount());
    });

This is the part of activity's xml:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/adView"
    android:layout_below="@id/userContainer"
    android:background="#CCC"
    android:padding="15dp">

    <LinearLayout
        android:id="@+id/messageList_LinearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <Button
            android:id="@+id/addButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:backgroundTint="@color/design_default_color_primary"
            android:text="ADD"
            android:textColor="@android:color/white" />
    </LinearLayout>
</ScrollView>

This is item_message.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="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left" />

    <EditText
        android:layout_width="300dp"
        android:layout_height="wrap_content" />

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_gravity="right"
        android:background="#FFD9D9" />
</LinearLayout>

So, I'd like to make a view with item_message and put it into LinearLayout in the activity's xml, which has messageList_LinearLayout for the id attribute.

The button is also in the LinearLayout. The button must be placed at the end of the LinearLayout.

And whenever, I click the button, a message view must be created and added at the end of the messageList and also before the button.

c-an
  • 3,543
  • 5
  • 35
  • 82

1 Answers1

1

You can just change the third parameter into null.

View messageView = getLayoutInflater().inflate(this, R.layout.item_message, null);
messageListLL.addView(messageView, messageListLL.getChildCount() - 1)

The third parameter specifies its root view. You can inflate the messageView without any roots.

c-an
  • 3,543
  • 5
  • 35
  • 82