Using android Inflate outside of onCreateView
Asked Answered
C

1

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

Compendium answered 9/10, 2014 at 20:32 Comment(1)
This seems like something you should just use a separate Fragment for. If the UI is entirely different, you should just replace the Fragment.Nonie
E
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);
}
Endmost answered 9/10, 2014 at 20:35 Comment(5)
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?Compendium
@Compendium 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.Endmost
@Endmost 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.Thankyou
@Thankyou in this case inflating doesn't add the view to the hierarchy. You need to do it manually afterwards, e.g. mContainer.addView(mView);Endmost
@Endmost 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 for more information. Thanks.Thankyou

© 2022 - 2024 — McMap. All rights reserved.