How to add Action Bar in Android Applications?

In this post, I will teach you how to add Action Bar in Android Applications. I will also discuss how to make the Action Bar work in lower SDK Android version (Backward compatibility).

Prerequisite:

You must know about Action Bar before you start creating it. I recommend you to take a look at What is Action Bar? post which will provide you basic information about Action Bar.

Adding the Action Bar in Android applcation involves three steps, they are:

  1. Create Android Application Project with Holo Theme
  2. Setting up the Action Bar for lower SDK versions
  3. Adding Action Buttons


You can download source code from here if you don’t want to create Application from scratch, otherwise please proceed with below listings.


Create Android Application Project with Holo Theme

Step 1:

  • Create new android project [File >> New >> Android Application Project] with project name AndroidActionBar
  • Enter package name as ‘com.prgguru.example’
  • Choose Minimum Required SDK, Target SDK and Compile with as shown in the below screen=print. If you don’t have latest SDK installed, they will not be populated in the dropdownlist, I would recommend you to update Android SDK manager with latest SDKs. Confused on choosing these options? Take a look at Minimum Required SDK – Target SDK – Compile With post.
  • Select Theme as ‘Holo Light with Dark Action Bar’ which will create Action Bar by default in Android 3.0 and above.
  • Click Next button

ActionBar_1

Step 2:

Uncheck ‘Create custom launcher icon’ and Click Next button.

ActionBar_2

Step 3:

Select ‘Blank Activity’ and Click Next button.

ActionBar_3

Step 4:

Leave the default values and Click Finish button.

ActionBar_4

Setting up the Action Bar for lower SDK versions

Next step is to set up Action Bar for lower SDK versions. Setting up of Action bar is divided into two sections depending on the Android version the application going to support.

Android 3.0 and above

From Android 3.0 (API level 11), Action Bar is included in all activities that use Theme.Holo theme or custom theme created using Theme.Holo as parent theme.

That’s it, Action Bar will be added if your Application is targeted for Android version 3.0 and above as Theme.Holo theme is supported by these versions.

Tip:

Make sure you set either the targetSdkVersion or minSdkVersion attribute to “11” or greater in AndroidManifest.xml so that the theme ‘Theme.Holo’ will be supported in targeted devices which in turn add Action Bar by default.

Support Android 2.1 and Above

Adding Action Bar when running on versions older than Android 3.0 requires us to include Android support library.

Warning:

You must download Android support libraries in order to add Action Bar in lower SDK version. I would recommend you to install all latest updates showing in Android SDK manager which will install Android support libraries. Once after you installed the updates, don’t forget to check if there are any updates available for ADT plugin by clicking on ‘Install New Software’ present under ‘Help’ menu in Eclipse.

Steps to add Action Bar in Android 2.1 and above versions:

  1. Include ‘android-support-v7-appcompat.jar’ in lib folder present under Application project folder. This library is located in the <SDK>/extras/android/support/v7/appcompat/ directory after you download the Android Support Libraries.
  2. Your activity should inherit ActionBarActivity class which is bundled in v7appcompat JAR.
  3. Your application or individual activity theme should have android.theme attribute set to “@style/Theme.AppCompat.Light” as shown below

    android:theme=”@style/Theme.AppCompat.Light”

Now your application or activity includes the action bar when running on Android 2.1 (API level 7) or higher.

Quick Tip:

If Android SDK and ADT plugin are updated with all latest updates, ADT plugin will take care of including v7appcompat.jar and inheriting ActionBarActivity when you choose Minimum Required Sdk Version as “7” while creating Android Application project.


Adding Action Buttons

Action Bar allows us to add buttons for the most often used Action items.

Open strings.xml present under values folder and paste below XML:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">AndroidActionBar</string>
    <string name="hello_world">Hello world!</string>
    <string name="search">Search</string>
    <string name="products">Products</string>
    <string name="contactus">Contact Us</string>
    <string name="about">About</string>
</resources>

Open main.xml present under menu folder and paste below XML:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.prgguru.example.MainActivity" >

    <item
        android:id="@+id/search"
        android:orderInCategory="100"
        android:title="@string/search"
        app:showAsAction="always"
        android:icon="@drawable/ic_action_search"
        />
     <item
        android:id="@+id/products"
        android:orderInCategory="100"
        android:title="@string/products"
        app:showAsAction="ifRoom"/>
    <item
        android:id="@+id/contact_us"
        android:orderInCategory="100"
        android:title="@string/contactus"
        app:showAsAction="never"/>
    <item
        android:id="@+id/about"
        android:orderInCategory="100"
        android:title="@string/about"
        app:showAsAction="never"/>

</menu>

Action Items we created is already as added in MainActivity.java as shown below:

@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

[pglinkadssmall1]

Demo

That’s all. It’s time to test our code.

Run the application using emulator or device by right clicking on the project >> Run as >> Android applicaiton >> Choose emulator or device.



Download Source Code

Entire project is zipped and is available for download. Unzip the downloaded project and to import the project into eclipse, launch eclipse >> File >> Import.. >> Choose downloaded project(How to import android project in eclipse). If you just want to run the application in your mobile and see the output but don’t want to hit your head with source code, download application(apk) file and install it in your mobile device.

Download Source Code Download Application(apk)

*apk in Android  is the installation file similar to exe in windows.

[pglinkadssmall]

If you feel this article is helpful and interesting please spread a word about it to your friends and colleagues by sharing the article in Facebook or Twitter.


Share

You are always welcome to provide your comments and feedback from comment box.

Next Tutorial >> How to handle clicks on Action Bar action items?

Reference
Adding Action Bar in Android Applications
[pgwriteforus]
[pgfeedback]

Related Articles

Author: Udhay

Share This Post On
468 ad
Join now to get more access to our Android Tutorials

Join now to get more access to our Android Tutorials

Once you Join, you will receive Mail containing latest Android Tutorials once a month !!

I promise I wont spam you !!

You have Successfully Subscribed!

Pin It on Pinterest

Shares