This lesson teaches you to
You should also read
In its most basic form, the action bar displays the title for the activity and the app icon on the left. Even in this simple form, the action bar is useful for all activities to inform users about where they are and to maintain a consistent identity for your app.
Setting up a basic action bar requires that your app use an activity theme that enables the action bar. How to request such a theme depends on which version of Android is the lowest supported by your app. So this lesson is divided into two sections depending on which Android version is your lowest supported.
Support Android 3.0 and Above Only
Beginning with Android 3.0 (API level 11), the action bar is included in all
activities that use the Theme.Holo
theme (or one of its
descendants), which is the default theme when either the targetSdkVersion
or
minSdkVersion
attribute is set to "11"
or greater.
So to add the action bar to your activities, simply set either attribute to
11
or higher. For example:
<manifest ... > <uses-sdk android:minSdkVersion="11" ... /> ... </manifest>
Note: If you've created a custom theme, be sure it uses one
of the Theme.Holo
themes as its parent. For details,
see Styling the Action Bar.
Now the Theme.Holo
theme is applied to your app and
all activities show the action bar. That's it.
Support Android 2.1 and Above
Adding the action bar when running on versions older than Android 3.0 (down to Android 2.1) requires that you include the Android Support Library in your application.
To get started, read the Support Library Setup document and set up the v7 appcompat library (once you've downloaded the library package, follow the instructions for Adding libraries with resources).
Once you have the Support Library integrated with your app project:
- Update your activity so that it extends
ActionBarActivity
. For example:public class MainActivity extends ActionBarActivity { ... }
- In your manifest file, update either the
<application>
element or individual<activity>
elements to use one of theTheme.AppCompat
themes. For example:<activity android:theme="@style/Theme.AppCompat.Light" ... >
Note: If you've created a custom theme, be sure it uses one of the
Theme.AppCompat
themes as its parent. For details, see Styling the Action Bar.
Now your activity includes the action bar when running on Android 2.1 (API level 7) or higher.
Remember to properly set your app's API level support in the manifest:
<manifest ... > <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="18" /> ... </manifest>