I am adding layouts on a button click.
private void addLayout() {
layout2 = LayoutInflater.from(mContext).inflate(R.layout.product_layout, mLinearLayout, false);
productAuto = (AutoCompleteTextView) layout2.findViewById(R.id.tv_product);
qtyEditText = (EditText) layout2.findViewById(R.id.prod_qty);
prodPriceEditText = (EditText)layout2.findViewById(R.id.prod_price);
prodSpecsEditText = (EditText)layout2.findViewById(R.id.prod_specs);
removeProduct = (Button)layout2.findViewById(R.id.btn_rmv);
mLinearLayout.addView(layout2);
setProd();
this.initListenersPrd(getActivity());
}
private void initListenersPrd(final Context context) {
productAuto.setOnItemClickListener((parent, view, position, id) -> {
String newName = parent.getItemAtPosition(position).toString();
ProductDetail productDetail = mProductManager.getProductByPrdName(newName);
if (productDetail != null) {
this.prodPriceEditText.setText(productDetail.getPrice());
this.prodSpecsEditText.setText(productDetail.getProductSpec());
}
});
removeProduct.setOnClickListener(v -> {
if (mLinearLayout.getChildCount()>0)
{
((LinearLayout)mLinearLayout.getParent()).removeView(mLinearLayout);
}
});}
Now I want to remove a particular product form from the application so I have done ((LinearLayout)mLinearLayout.getParent()).removeView(mLinearLayout); on remove button click. But it removes the entire layouts that are added dynamically.
GUI
As you can see in the above image I have added 4-5 layouts of the product but when I click the remove button I remove all the layouts.
Update 1
I have added the below layout in my main layout and then called the product layout inside it programatically
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_prod"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
The product layout is created separately and I am calling it in my main layout.
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2014 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/ll_out"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background_round"
android:orientation="vertical"
android:padding="5sp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10sp"
android:orientation="horizontal">
<AutoCompleteTextView
android:id="@+id/tv_product"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:gravity="left"
android:hint="Enter Product"
android:inputType="text" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10sp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:orientation="vertical">
<EditText
android:id="@+id/prod_qty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:editable="false"
android:focusable="true"
android:focusableInTouchMode="true"
android:hint="Enter Quantity"
android:gravity="left"
android:inputType="number" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:orientation="vertical">
<EditText
android:id="@+id/prod_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:editable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:hint="Prod Price"
android:gravity="left"
android:inputType="none" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:orientation="vertical">
<EditText
android:id="@+id/prod_specs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:editable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="left"
android:hint="Prod Specs"
android:inputType="none" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:layout_marginTop="1dp"
android:padding="0dp">
<Button
android:id="@+id/btn_rmv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Remove Product"
android:textColor="@color/white" />
</LinearLayout>
</LinearLayout>
The above layout is called whenever I click on Add New Product Button.
How can I remove a particular layout??
Any help would be highly appreciated.

