Jacksonkr
Jacksonkr

Reputation: 32207

android remove all buttons inside a view

I have a LinearLayout which I've looped a number of new Button objects into. How do I go about clearing that div correctly (eg removing all the buttons)? I've tried a number of times (unsuccessfully) to do this, but have nothing to show for it.

** edit **

I'm not sure if this helps, but in flex/AS3 I would do something like:

while(myView.numChildren) myView.removeChildAt(0);

** a little code **

View col1 = findViewById(R.id.col1);
for(final Map.Entry<String,HashMap<String,String>> entry : _nav.entrySet()) {
    Button item = new Button(this);
    item.setText(entry.getKey());
    item.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            openCol2(entry);
        }
    });
    ((LinearLayout) col1).addView(item);
}

private final void openCol2(Map.Entry<String,HashMap<String,String>> entry) {
    View col2 = findViewById(R.id.col2);
    // here is where I want to clean out col2. Right before I add more buttons.

    for(int i = 0; i < _nav.size(); ++i) {
        Button item = new Button(this);
        //item.setText(entry.getKey());
        ((LinearLayout) col2).addView(item);
    }
}

Upvotes: 1

Views: 5857

Answers (1)

Gopal
Gopal

Reputation: 1719

Try this

LinearLayout col2 = (LinearLayout)findViewById(R.id.col2);
col2.removeAllViews();

Assumption: R.id.col2 is of LinearLayout type else to make it more generic typecast it to ViewGroup. Hope this help!!!

Upvotes: 9

Related Questions