2
   public void Add_text() {


        ll.setOrientation(LinearLayout.HORIZONTAL);
        ll.setId(i);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        TextView product = new TextView(getActivity());
        product.setText(" Product" + 5 + "    ");
        ll.addView(product);

        EditText qty = new EditText(getActivity());
        qty.setText(i + "");
        qty.setId(i);
        qty.setWidth(120);

        ll.addView(qty);

        Button btn = new Button(getActivity());
        ll.addView(btn);
        btn.setLayoutParams(params);
        btn.setOnClickListener(o);

        ly.addView(ll);
        i++;

    }

I wrote the above code to create the textfields and buttons dynamically; But now I need to remove 2 textfields and a button when the button is clicked. How do I do that?

rg88
  • 20,742
  • 18
  • 76
  • 110
user2771059
  • 298
  • 1
  • 4
  • 12

6 Answers6

5

Try following code.

button.setOnClickListener(new View.OnClickListener() {      
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        LinearLayout linearParent =  (LinearLayout) v.getParent().getParent();
        LinearLayout linearChild = (LinearLayout) v.getParent();
        linearParent.removeView(linearChild);   
    }
});

Explanation

  • Here first take "GrandParent" of any view.
  • Then take its "Parent" view
  • With reference to "GrandParent" remove that "Parent" view.
  • this will remove all views which that "Parent" holds. As per your code, your "ll" will be "linearChild" here. And "ly" will be "linearParent" here. So whole "ll" will be removed from "ly" which you have added dynamically.
Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93
1

If you want to permanently remove the views you created.

  OnClick(View view){
     ly.removeAllViews()
  }

If you do not want to permanently remove the views you created.

 OnClick(View view){
     ly.setVisibility(View.GONE); //This will hide the all views
     qty.setVisibility(View.GONE);//This will hide the EditText qty 
     product .setVisibility(View.GONE);//This will hide the TextView product 
 }

So use appropriate code line which you want.

EDIT:

Use this code for your situation:

public void Add_text() {


    ll.setOrientation(LinearLayout.HORIZONTAL);
    ll.setId(i);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    TextView product = new TextView(getActivity());
    product.setText(" Product" + 5 + "    ");
    ll.addView(product);

    EditText qty = new EditText(getActivity());
    qty.setText(i + "");
    qty.setId(i);
    qty.setWidth(120);

    ll.addView(qty);

    Button btn = new Button(this);
    ll.addView(btn);
    btn.setLayoutParams(params);


    ly.addView(ll);
    i++;

    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View button) {
            qty.setVisibility(View.GONE);//This will hide the EditText qty 
             product .setVisibility(View.GONE);//This will hide the TextView product 

        }
    });

}
Dinesh Anuruddha
  • 7,137
  • 6
  • 32
  • 45
  • Actully i dont want remove all the items.Ineed to remove the textfields that asssociated with the buttons. – user2771059 Jan 30 '14 at 05:49
  • @kaluwila Dude I am just wondering what is the difference b/w View.GONE and View.INVISIBLE. can u tell me? – SMR Jan 30 '14 at 05:58
  • @SMR : check http://stackoverflow.com/questions/11556607/android-difference-between-invisible-and-gone and u will understand what is the difference – Kaushik Jan 30 '14 at 06:01
  • In ur answer if i create more than 1 rows and try to remove that randmly this method does not work.Eg.I create 5 rows and i need to remove the 3 rd row when the dynmically created 3 rd button is clicked – user2771059 Jan 30 '14 at 06:14
  • @user2771059 i have edited the answer hope this will solve your problem. – Dinesh Anuruddha Jan 30 '14 at 06:14
0

i think you got to use this method on your LinearLayout :

public void removeView (View view)

first you call :

EditText et = (EditText)linearLayout.findViewById(yourEditText.getId()); 

then call the remove view method :

linearLayout.removeView (et) ; 

and to remove all of the Views that are in the LinearLayout do the following :

public void removeAllViews ()

like the following :

linearLayout.removeAllViews()

and give me some feedback

Hope that Helps .

0

you can simply use qty.setVisibility(View.GONE) on the onClickListener() of the Button of your choice. like this.

btn.setOnClickListener(new OnClickListener() {  
    @Override
    public void onClick(View v) {
        qty.setVisibility(View.GONE); //for temporary purpose

        //or u can also do this
        layout.removeView(qty); //removes permanently
    }
});

The benefit of using View.GONE is that you can get the View back if you want but layout.removeView(qty) will remove the view permanently and you have to re add the view again.

[EDIT] 1. changed to View.GONE instead of View.INVISIBLE because of reasons explained here


Hope I answered your question. :)

Community
  • 1
  • 1
SMR
  • 6,628
  • 2
  • 35
  • 56
0

just use index for which you want to remove your view from linear layout

Linearlayout.removeViewAt();

if you want again that view then you can call addViewAt()in same way.

I hope it will help you.

0

Add this line

mLayout.removeViewAt(mLayout.getChildCount()-1);
Karan Chunara
  • 518
  • 4
  • 15