page.title=Setting Up the Action Bar page.tags=actionbar helpoutsWidget=true trainingnavtop=true @jd:body
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.
Figure 1. An action bar with the app icon and activity title.
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.
Beginning with Android 3.0 (API level 11), the action bar is included in all
activities that use the {@link android.R.style#Theme_Holo Theme.Holo} theme (or one of its
descendants), which is the default theme when either the {@code targetSdkVersion} or
{@code minSdkVersion}
attribute is set to "11"
or greater.
So to add the action bar to your activities, simply set either attribute to {@code 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 {@link android.R.style#Theme_Holo Theme.Holo} themes as its parent. For details, see Styling the Action Bar.
Now the {@link android.R.style#Theme_Holo Theme.Holo} theme is applied to your app and all activities show the action bar. That's it.
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:
public class MainActivity extends ActionBarActivity { ... }
<activity android:theme="@style/Theme.AppCompat.Light" ... >
Note: If you've created a custom theme, be sure it uses one of the {@link android.support.v7.appcompat.R.style#Theme_AppCompat 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>