giozh
giozh

Reputation: 10068

Remove button from inside Fragment

I have a Fragment that contains some Widget(Button, TextView and something else) all declared inside it's xml layout file. Now if a condition is verified, I would remove one of that widget from Fragment layout (not just hide, but remove). It's possible to do it programmatically from onCreateView method?

Upvotes: 0

Views: 1147

Answers (4)

sergej shafarenka
sergej shafarenka

Reputation: 20416

You can definitely do this.

public void onViewCreated(View view, Bundle savedInstanceState) {
    ViewGroup parent = (ViewGroup) getView();
    parent.removeView(parent.findViewById(R.id.view_to_remove));
}

Upvotes: 1

Francesco verheye
Francesco verheye

Reputation: 1574

You can use something like this: Button myButton = view.findViewById(R.id.mybutton); ((LinearLayout)myButton.getParent()).removeView(myButton);

Upvotes: 1

2Dee
2Dee

Reputation: 8627

Provided that you get a reference to that Button inside onCreateView, you can set the visibility of that Button to GONE, which I believe is the effect you're trying to achieve :

button.setVisibility(View.GONE);

Upvotes: 2

Nathua
Nathua

Reputation: 8826

after you inflate your layout in onCreateView

container.removeView(container.findViewById(R.id.your_widget));

Upvotes: 1

Related Questions