Search code examples
androidandroid-actionbarswipe

How do I add an action bar to a swipe view?


I've been following http://developer.android.com/training/implementing-navigation/lateral.html to create a swipe view with fragments.

In particular, I've taken the block of code

public class CollectionDemoActivity extends FragmentActivity {
    // When requested, this adapter returns a DemoObjectFragment,
    // representing an object in the collection.
    DemoCollectionPagerAdapter mDemoCollectionPagerAdapter;
    ViewPager mViewPager;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collection_demo);

        // ViewPager and its adapters use support library
        // fragments, so use getSupportFragmentManager.
        mDemoCollectionPagerAdapter =
                new DemoCollectionPagerAdapter(
                        getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mDemoCollectionPagerAdapter);
    }
}

// Since this is an object collection, use a FragmentStatePagerAdapter,
// and NOT a FragmentPagerAdapter.
public class DemoCollectionPagerAdapter extends FragmentStatePagerAdapter {
    public DemoCollectionPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        Fragment fragment = new DemoObjectFragment();
        Bundle args = new Bundle();
        // Our object is just an integer :-P
        args.putInt(DemoObjectFragment.ARG_OBJECT, i + 1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getCount() {
        return 100;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return "OBJECT " + (position + 1);
    }
}

// Instances of this class are fragments representing a single
// object in our collection.
public static class DemoObjectFragment extends Fragment {
    public static final String ARG_OBJECT = "object";

    @Override
    public View onCreateView(LayoutInflater inflater,
            ViewGroup container, Bundle savedInstanceState) {
        // The last two arguments ensure LayoutParams are inflated
        // properly.
        View rootView = inflater.inflate(
                R.layout.fragment_collection_object, container, false);
        Bundle args = getArguments();
        ((TextView) rootView.findViewById(android.R.id.text1)).setText(
                Integer.toString(args.getInt(ARG_OBJECT)));
        return rootView;
    }
}

and extended it to suit my needs.

How do I modify this so that there's an action bar on the top of the screen, with only the name of the activity (in the center), and the app icon and an arrow to return the parent activity (to the left)?

I've tried adding

final ActionBar actionBar = getActionBar();
actionBar.setHomeButtonEnabled(true);

to the CollectionDemoActivity onCreate in the code above, but this causes my app to crash.

Edit:

My styles.xml includes

<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
<style name="AppTheme" parent="AppBaseTheme">

My manifest includes

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

...with activity tags inside the application. None of the activities override the android:theme set at the application level. The parent activity has an action bar, but the child (the swipe activity that I'm asking about in this post) does not.


Solution

  • Your problem is very simple in its nature as it has little to do with the swipe view: you simply want an ActionBar in your FragmentActivity.

    The documentation makes a special note about using the ActionBar in a FragmentActivity:

    Note: If you want to implement an activity that includes an action bar, you should instead use the ActionBarActivity class, which is a subclass of this one, so allows you to use Fragment APIs on API level 7 and higher.

    Have your activity extend ActionBarActivity, and obtain a reference to the ActionBar using getSupportActionBar().