Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using android Inflate outside of onCreateView

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

like image 470
Jonathan Avatar asked Mar 19 '23 06:03

Jonathan


1 Answers

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);
}
like image 109
Simas Avatar answered Mar 26 '23 04:03

Simas