5

I have a fragment. In my On create I do set my inflater as follows.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    v= inflater.inflate(R.layout.dashboard_two, container, false);  
    getActivity().getActionBar().setTitle("NV Technologies");
    new HttpRequestFaultList().execute();
    return v;
}   

I want to change the inflater ti display A different Layout outside of onCreate view. I have tried:

v= inflater.inflate(R.layout.custom_dash, container, false);

custom_dash.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:id="@+id/testFrag"
  android:layout_height="match_parent"
  android:orientation="vertical" >

   <ImageView
      android:id="@+id/imageView1"
      android:layout_width="269dp"
      android:layout_height="387dp"
      android:layout_gravity="center"
      android:scaleType="centerInside"
      android:src="@drawable/message" />

</LinearLayout>

But because it is outside of the onCreate there does not exist an inflater or a container. I want to change the Layout in a catch block and if there is a catch the Layout must change. Is it even possible to change the layout in the Fragment but outside of the onCreate method? And how

Jonathan
  • 545
  • 1
  • 8
  • 27
  • This seems like something you should just use a separate Fragment for. If the UI is entirely different, you should just replace the Fragment. – Kevin Coppock Oct 09 '14 at 22:22

1 Answers1

8

You can inflate from within your fragment like this:

FrameLayout container = (FrameLayout) getActivity().findViewById(R.id.fragment_container);
LayoutInflater.from(getActivity())
        .inflate(R.layout.custom_dash, container, false);

If that doesn't work out for you (for some reason), you can always save references to both the inflater and the container in your onCreateView method and use them whenever:

private LayoutInflater mInflater;
private ViewGroup mContainer;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mInflater = inflater;
    mContainer = container;
    v = inflater.inflate(R.layout.dashboard_two, container, false);  
    getActivity().getActionBar().setTitle("NV Technologies");
    new HttpRequestFaultList().execute();
    return v;
}

public View someOtherMethod() {
    mInflater.inflate(R.layout.custom_dash, mContainer, false);
}
Simas
  • 43,548
  • 10
  • 88
  • 116
  • What do you mean with fragment container. I placed the custom_dash.xml in my question. Is it one of the fields in that xml? – Jonathan Oct 09 '14 at 20:52
  • @Jonathan fragment_container is the id of the view that holds you fragment. It should be located in you activity's layout and probably is a FrameLayout. Updated my answer. – Simas Oct 09 '14 at 20:54
  • @Simas How would you implement `someOtherMethod()`? If I try to reinflate a `View` in `onResume()`, doing something like: `mView = someOtherMethod();` has no effect. The `View` doesn't inflate. – pez Jul 16 '15 at 04:21
  • 2
    @pez in this case inflating doesn't add the view to the hierarchy. You need to do it manually afterwards, e.g. mContainer.addView(mView); – Simas Jul 16 '15 at 05:23
  • @Simas Doesn't seem to work for me, because it seems like the `View` doesn't actually inflate. If you're interested, see my question [here](http://stackoverflow.com/questions/31359994/swap-nested-fragments-onresume) for more information. Thanks. – pez Jul 17 '15 at 03:35