Moeez
Moeez

Reputation: 478

Remove layout on a button click programmatically in android studio

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

enter image description here

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.

Upvotes: 5

Views: 866

Answers (3)

Vanshaj Daga
Vanshaj Daga

Reputation: 2165

Using RecyclerView with Adapter is your best bet in these situations. My working code is as followes

Class listAdapter

package com.akshita.recycler;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class listAdapter extends RecyclerView.Adapter<listAdapter.ViewHolder> {
    private ArrayList<String> product_name;
    private ArrayList<Integer> quantity;
    private ArrayList<Double> price;
    private ArrayList<String> specs;
    private static Context scontext;

    public listAdapter(Context context)
    {
        scontext = context;
        this.product_name = new ArrayList<String>();
        this.price = new ArrayList<Double>();
        this.quantity = new ArrayList<Integer>();
        this.specs = new ArrayList<String>();
    }

    public listAdapter(Context context, ArrayList<String> pdname, ArrayList<Integer> quantity, ArrayList<Double> price, ArrayList<String> specs)
    {
        this.product_name = pdname;
        this.price = price;
        this.quantity = quantity;
        this.specs = specs;
        scontext = context;
    }

    @NonNull
    @Override
    public listAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater =  LayoutInflater.from(parent.getContext());
        View listItem = layoutInflater.inflate(R.layout.productview,parent,false);
        ViewHolder viewHolder = new ViewHolder(listItem);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull listAdapter.ViewHolder holder, int position) {
        holder.actv.setText(product_name.get(position));
        holder.qty.setText(quantity.get(position).toString());
        holder.price.setText(price.get(position).toString());
        holder.desc.setText(specs.get(position));

    }
    public void addView(String pdname, Double prc, Integer quant, String spec ) {

        product_name.add(pdname);
        quantity.add(quant);
        price.add(prc);
        specs.add(spec);
        notifyItemInserted(product_name.size());
        notifyItemInserted(quantity.size());
        notifyItemInserted(price.size());
        notifyItemInserted(specs.size());
    }

    public void removeAt(int position) {

        product_name.remove(position);
        quantity.remove(position);
        price.remove(position);
        specs.remove(position);
        notifyItemRemoved(position);
        notifyItemRangeChanged(position, product_name.size());
        notifyItemRangeChanged(position, quantity.size());
        notifyItemRangeChanged(position, price.size());
        notifyItemRangeChanged(position, specs.size());
    }

    @Override
    public int getItemCount() {
        return product_name.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        public AutoCompleteTextView actv;
        public EditText qty;
        public EditText price;
        public EditText desc;
        public Button button;
        public ViewHolder(View itemView) {
            super(itemView);
            this.actv = itemView.findViewById(R.id.tv_product);
            this.qty = itemView.findViewById(R.id.prod_qty);
            this.price = itemView.findViewById(R.id.prod_price);
            this.desc = itemView.findViewById(R.id.prod_specs);
            this.button = itemView.findViewById(R.id.btn_rmv);
            button.setOnClickListener(this);
        }


        @Override
        public void onClick(View v) {
            if(v.equals(button)){
                removeAt(getAdapterPosition());
        }

        }
    }
}

My Main Class

MainActivity.java

package com.akshita.recycler;

import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    listAdapter adapter;
    RecyclerView hs;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        hs = findViewById(R.id.rcview);

        hs.setHasFixedSize(false);
        hs.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
    }

    @Override
    public void onClick(View v) {
        switch(v.getId())
        {
            case R.id.add: addView(v);
        }
    }

    public void addView(View v)
    {
        if(adapter == null) {
            adapter = new listAdapter(this);
            hs.setAdapter(adapter);
        }
        adapter.addView("Name",0.0, 0, "Specs");
    }
}

productview.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:orientation="vertical">
    <LinearLayout
        android:id="@+id/ll_out"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        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>
</LinearLayout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:gravity="center|top"
    android:orientation="vertical">
    <Button
        android:id="@+id/add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="addView"
        />
    <androidx.recyclerview.widget.RecyclerView
        android:layout_marginTop="10dp"
        android:id="@+id/rcview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
    />
</LinearLayout>

Whole working code is available here on GitHub

Upvotes: 1

Cheticamp
Cheticamp

Reputation: 62851

You are trimming too high in the view hierarchy. If the button is a direct child of the LinearLayout you want to remove, you can do something like the following in a fragment. (It is similar for an Activity.)

Add five sub-layouts to the top-level layout.

int addCount = 0;
LinearLayout mLinearLayout = binding.linearLayoutId;
for (int i = 0; i < 5; i++) {
    LinearLayout newLayout = new LinearLayout(getActivity());
    newLayout.setBackground(new ColorDrawable(0xFFAAAAAA));
    TextView tv = new TextView(getActivity());
    tv.setTextSize(24);
    tv.setText("Hello World! " + ++addCount);
    newLayout.addView(tv);
    Button button = new Button(getActivity());
    button.setText("Click me");
    newLayout.addView(button);
    mLinearLayout.addView(newLayout);
    ((LinearLayout.MarginLayoutParams) newLayout.getLayoutParams()).setMargins(margin, margin, margin, margin);
    button.setOnClickListener(v -> { // v is the button
        mLinearLayout.removeView((ViewGroup) v.getParent());
    });
}

Where the top layout is simply

<LinearLayout
    android:id="@+id/linear_layout_id"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/holo_blue_light"
    android:orientation="vertical"/>

In the following, I simply click the buttons for the odd layouts then the buttons for the even layouts. enter image description here

Upvotes: 1

Islam Assem
Islam Assem

Reputation: 1512

It's because you are removing the entire layout instead you should remove layout been added dynamically I'm supposing that remove product is a direct child in product_layout main view group

    removeProduct.setOnClickListener(v -> {
   if (mLinearLayout.getChildCount()>0)
   {
       ((LinearLayout)mLinearLayout.getParent()).removeView(removeProduct.getParent());
   }

});}

But you should keep in mind that this is totally not recommended and instead you should use recyclerview and on adding new product add an object to products list and call notifyDataSetChanged(), also you can use System.currentTimeMillis() as a unique id for product object.

Upvotes: 0

Related Questions