Hick
Hick

Reputation: 36394

Not able to dynamically remove LinearLayout in Android. How to do it?

public void onClick_addContact(View v)
    {
            LinearLayout layout = (LinearLayout) findViewById(R.id.layoutLinear);
            layout.addView(linearlayout(_intMyLineCount));
            _intMyLineCount++;



    }


    private EditText editText(int _intID) {
            EditText editText = new EditText(this);
            editText.setId(_intID);
            editText.setHint("My lines");
            editText.setWidth(180);         

            editTextList.add(editText);
            return editText;
        }

       private TextView textView(int _intID)
        {
            TextView txtviewAll=new TextView(this);
            txtviewAll.setId(_intID);
            txtviewAll.setText("My lines:");        

            textviewList.add(txtviewAll);
            return txtviewAll;
        }

       private Button button(int _intID)
       {
           Button btn = new Button(this);
           btn.setId(_intID);
           btn.setTag("but1");
           btn.setOnClickListener(newContact);


           return btn;


       }

       OnClickListener newContact = new OnClickListener() {
                //onClick view
                public void onClick(View v) {


                    LinearLayout layout =  (LinearLayout)v.getParent();

                    layout.removeViewInLayout(v);
                   // setContentView(layout);

                        _intMyLineCount--;

                                    }

               };

       private LinearLayout linearlayout(int _intID)
        {
            LinearLayout LLMain=new LinearLayout(this);
            LLMain.setId(_intID);       
            LLMain.addView(textView(_intID));
            LLMain.addView(editText(_intID));
            LLMain.addView(button(_intID));
            LLMain.setOrientation(LinearLayout.HORIZONTAL);

            linearlayoutList.add(LLMain);
            return LLMain;

        } 

In the newOnContact Listener, only the child button gets deleted, but not the entire linear layout that includes the textView and EditText. How to do it?

Upvotes: 0

Views: 1313

Answers (3)

sravan
sravan

Reputation: 5333

Simply use GONE constant:

layout.setVisibility(LinearLayout.GONE)

then your linear layout will be invisible and it won't allocate space also.

Upvotes: 1

Blackbelt
Blackbelt

Reputation: 157437

Instead of setting the newContact onClickListener to the Button, try setting to LLMain LinearLayout. So the view that you receive in the onClick is the LinearLayout you want to remove, if I ve not misunderstood you.

Upvotes: 0

SK.
SK.

Reputation: 355

You can use layout.setVisibility(View.INVISIBLE); or layout.setVisibility(View.Gone); to remove the layout from UI.

View.INVISIBLE will hide the view, but it'll still take the space whereas View.GONE will remove the view as well as it won't take up any space in the UI.

Refer http://developer.android.com/reference/android/view/View.html#attr_android:visibility

Upvotes: 2

Related Questions