I’m trying to change the layout of a fragment during runtime under a particular condition.
The initial layout in inflated within the onCreateView():
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.cancel_video, null);
}
Then sometime later within the fragment code I would like to replace the initial layout with some other layout.
I’ve tried a few things so far; this is the latest that I have:
private void Something(){
if(checkLicenseStatus(licenseStatus, statusMessage)){
View vv = View.inflate(getActivity(), R.layout.play_video, null);
//more code
}
}
How can I accomplish this?
You cannot replace the fragment’s layout once it is inflated. If you need conditional layouts, then you either have to redesign your layout and break it down into even smaller elemens like
Fragments. Alternatively you can group all the layout elements into sub containers (likeLinearLayout), then wrap them all inRelativeLayout, position them so they overlay each other and then toggle the visibility of theseLinearLayouts withsetVisibility()when and as needed.