Android Fragment layout
February 23, 2012 ◦ 1 min ◦
I recently ran into a problem when using a FragmentTranscation to replace a LinearLayout with a Fragment. The Fragment that I was putting into the layout was not filling the view port properly. Luckily it was a fairly easy problem to fix.
FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
trans.replace(R.id.main_fragment, mFragment);
trans.commit();
The code above was creating a layout that had an intermediary FrameLayout that did not have the proper layout parameters. NoSaveStateFrameLayout was set to wrap content so the children were not filling the viewport.
Luckily the fix was just to change the code in onCreateView of the Fragment:
Incorrect:
View layout = inflater.inflate(R.layout.fragment_page_edit, null);
Correct:
View layout = inflater.inflate(R.layout.fragment_page_edit, container, false);