The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.

More Fun with Pagers

In earlier chapters, we saw basic uses of ViewPager, along with ways to show multiple pages at a time on larger screens. However, there are other ways to apply ViewPager and integrate it into the rest of your application, some of which we will examine in this chapter.


This chapter assumes that you have read the core chapters, particularly the one showing how to use ViewPager.

Hosting ViewPager in a Fragment

Classically, the primary restriction on ViewPager was that you could not both have ViewPager be in a fragment and have ViewPager host fragments as its pages. You could do one or the other, but not both simultaneously.

As noted in a previous chapter, Android 4.2 supports nested fragments natively, and the latest Android Support package backport also supports nested fragments. With those, you can have ViewPager be in a fragment and host fragments as its pages. However, it requires a minor modification to the way we set up a PagerAdapter, as is illustrated in the ViewPager/Nested sample project. This is the same project as ViewPager/Indicator, with the twist that the pages are fragments and the ViewPager is inside a fragment.

Our activity now implements the standard add-the-fragment-if-it-does-not-exist pattern that we have seen previously:


import android.os.Bundle;

public class ViewPagerIndicatorActivity extends FragmentActivity {
  public void onCreate(Bundle savedInstanceState) {

    if (getSupportFragmentManager().findFragmentById( == null) {
                                      new PagerFragment()).commit();

This loads a PagerFragment, which contains most of the logic from our original activity:


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class PagerFragment extends Fragment {
  public View onCreateView(LayoutInflater inflater,
                           ViewGroup container,
                           Bundle savedInstanceState) {
    View result=inflater.inflate(R.layout.pager, container, false);
    ViewPager pager=(ViewPager)result.findViewById(;



  private PagerAdapter buildAdapter() {
    return(new SampleAdapter(getActivity(), getChildFragmentManager()));

The biggest difference is that our call to the constructor of SampleAdapter no longer uses getSupportFragmentManager(). Instead, it uses getChildFragmentManager(). This allows SampleAdapter to use fragments hosted by PagerFragment, rather than ones hosted by the activity as a whole.

No other code changes are required, and from the user’s standpoint, there is no visible difference.

Pages and the Action Bar

The preview of this section is unavailable right now, but if you leave your name and number at the sound of the tone, it might get back to you (BEEEEEEEEEEEEP!).

ViewPagers and Scrollable Contents

The preview of this section was lost in the sofa cushions.

Columns for Large, Pages for Small

The preview of this section was eaten by a grue.

Introducing ArrayPagerAdapter

The preview of this section is out seeking fame and fortune as the Dread Pirate Roberts.

Columns for Large Landscape, Pages for the Rest

The preview of this section apparently resembled a Pokémon.

Adding, Removing, and Moving Pages

The preview of this section is [REDACTED].

Inside ArrayPagerAdapter

The preview of this section was eaten by a grue.