leshka
leshka

Reputation: 1794

Android: Fragments: setContentView alternative

I'm trying to rewrite an existing project using fragments to port my application on tablet.

I'm replacing activities with fragments.

The problem is that I don't know what is the equivalent to setContentView method? Is there any way to create Fragment's view except rewriting onCreateView?

Upvotes: 26

Views: 20639

Answers (2)

ASH
ASH

Reputation: 2758

In the onCreateView() method you can return the view of your fragment as follows :

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{ 
  return inflater.inflate(R.layout.ads_tab, container, false); 
}

Upvotes: 39

inazaruk
inazaruk

Reputation: 74790

I'm not sure why you cannot use onCreateView. But here is what you can do.

Create LinearLayout object, save it as mRooLayout member field and return it from onCreateView. Here is how you can implement setContentView:

void setFragmentContentView(View view)
{
    LinearLayout.LayoutParams layoutParams = 
            new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 
                                          ViewGroup.LayoutParams.FILL_PARENT);
    mRootLayout.removeAllViews();
    mRootLayout.addView(view, layoutParams);
}

Upvotes: 3

Related Questions