Skip to content

Common Navigation Paradigms

Nathan Esquenazi edited this page Mar 12, 2014 · 9 revisions

Overview

Navigation between views is an important part of any application. There are several ways to setup navigation in an Android application:

  • ActionBar - Using the ActionBar and/or ActionBar tabs to switch between different views
  • Swipe Views - Allow paging between views using a swipe gesture
  • Navigation Drawer - Displays a vertical menu that slides in to allow navigation between views
  • Screen Map - Providing a series of buttons on screen that can be pressed to visit different views

These four represent the most common navigation paradigms in Android applications. The specifics for how to implement these can be found in the various links above.

ActionBar Tabs

Check out the ActionBar Tabs cliffnotes for more details of how to add ActionBar tabs to your Activity.

Swiping Views

Check out the ViewPager with FragmentPagerAdapter cliffnotes for more details of how to add swipe-able views as a form of navigation.

Navigation Drawer

To create a basic navigation drawer that toggles between displaying different fragments, check out the Fragment Navigation Drawer cliffnotes. For more details about creating a custom drawer check out the Creating a Navigation Drawer docs.

Although both of the navigation drawer examples show how fragments can be substituted with the navigation drawer, you can also use RelativeLayout/LinearLayout if you wish to use the drawer as an overlay to your currently displayed Activity.

Instead of <FrameLayout> you can subsitute for <LinearLayout>

<android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
                                        android:layout_width="match_parent"
                                        android:layout_height="match_parent"
                                        android:id="@+id/drawer_layout">

        <LinearLayout
                android:id="@+id/content_frame"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

        <!-- The navigation drawer -->
        <ListView android:id="@+id/left_drawer"
                  android:layout_width="240dp"
                  android:layout_height="wrap_content"
                  android:layout_gravity="start"
                  android:choiceMode="singleChoice"
                  android:divider="@android:color/transparent"
                  android:dividerHeight="0dp"
                  android:background="#111"/>
</android.support.v4.widget.DrawerLayout>

Instead of:

 // Insert the fragment by replacing any existing fragment
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction()
                   .replace(R.id.content_frame, fragment)
                   .commit();

You can also use the LinearLayout container to inflate the Activity:

LayoutInflater inflater = getLayoutInflater();
LinearLayout container = (LinearLayout) findViewById(R.id.content_frame);
inflater.inflate(R.layout.activity_main, container);

References

Finding these guides helpful?

We need help from the broader community to improve these guides, add new topics and keep the topics up-to-date. See our contribution guidelines here and our topic issues list for great ways to help out.

Check these same guides through our standalone viewer for a better browsing experience and an improved search. Follow us on twitter @codepath for access to more useful Android development resources.

Clone this wiki locally