Android Programming The Big Nerd Ranch Guide by Bill Phillips, Chris Stewart, Kristin Marsicano (z-lib.org)

(gtxtreme123) #1

Creating a layout with two fragment containers


Creating a layout with two fragment containers


In the project tool window, right-click res/layout/ and create a new Android XML file. Ensure that
the resource type is Layout, name the file activity_twopane.xml, and give it a LinearLayout root
element.


Use Figure 17.4 to write the XML for the two-pane layout.


Figure 17.4  A layout with two fragment containers
(layout/activity_twopane.xml)


Note that the first FrameLayout has a fragment_container layout ID, so the code in
SingleFragmentActivity.onCreate(...) can work as before. When the activity is created, the fragment
that is returned in createFragment() will appear in the lefthand pane.


Test this layout in CrimeListActivity by overriding getLayoutResId() to return
R.layout.activity_twopane.


Listing 17.2  Changing to two-pane layout file (CrimeListActivity.java)


public class CrimeListActivity extends SingleFragmentActivity {


@Override
protected Fragment createFragment() {
return new CrimeListFragment();
}


@Override
protected int getLayoutResId() {
return R.layout.activity_twopane;
}
}

Free download pdf