Dacto
Dacto

Reputation: 2911

Set new layout in fragment

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?

Upvotes: 22

Views: 46079

Answers (4)

Marcin Orlowski
Marcin Orlowski

Reputation: 75629

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 (like LinearLayout), then wrap them all in RelativeLayout, position them so they overlay each other and then toggle the visibility of these LinearLayouts with setVisibility() when and as needed.

Upvotes: 17

wenmin92
wenmin92

Reputation: 106

I will change whole layout. You can change only a specific portion of your layout.

  1. Add a root FrameLayout in your_layout.xml, it contains nothing.

    <FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/fl_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    
  2. Set your content in the java code.

    ViewGroup flContent = findViewById(R.id.fl_content);
    
    private void setLayout(int layoutId) {
        flContent.removeAllViews();
        View view = getLayoutInflater().inflate(layoutId, flContent, false);
        flContent.addView(view);
    }
    
  3. You can change layoutId free at some point.
    If you have some listeners, you must set again.

Upvotes: 1

Ujjal Suttra Dhar
Ujjal Suttra Dhar

Reputation: 846

Yes, I have done this in following way. When I need to set a new layout(xml), the following code snippet should be executed.

  private View mainView;

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup containerObject, Bundle savedInstanceState){
    super.onCreateView(inflater, containerObject, savedInstanceState);

        mainView = inflater.inflate(R.layout.mylayout, null);
        return mainView;
  }

  private void setViewLayout(int id){
    LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mainView = inflater.inflate(id, null);
    ViewGroup rootView = (ViewGroup) getView();
    rootView.removeAllViews();
    rootView.addView(mainView);
  }

Whenever I need to change the layout I just call the following method

    setViewLayout(R.id.new_layout); 

Upvotes: 9

Tonithy
Tonithy

Reputation: 2290

Use a FragmentTransaction through the FragmentManger

FragmentManager fm = getFragmentManager();

if (fm != null) {
    // Perform the FragmentTransaction to load in the list tab content.
    // Using FragmentTransaction#replace will destroy any Fragments
    // currently inside R.id.fragment_content and add the new Fragment
    // in its place.
    FragmentTransaction ft = fm.beginTransaction();
    ft.replace(R.id.fragment_content, new YourFragment());
    ft.commit();
}

The code for the class YourFragment is just a LayoutInflater so that it returns a view

public class YourFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.your_fragment, container, false);

        return view;
    }   

}

Upvotes: 5

Related Questions