Reputation: 10068
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
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
Reputation: 1574
You can use something like this: Button myButton = view.findViewById(R.id.mybutton); ((LinearLayout)myButton.getParent()).removeView(myButton);
Upvotes: 1
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
Reputation: 8826
after you inflate your layout in onCreateView
container.removeView(container.findViewById(R.id.your_widget));
Upvotes: 1