Implementing Action Bar using ActionBarCompat support library in Android

July 25, 2013
By

We know that, action bar is implemented in Android 3.0 ( Honeycomb or API Level 11) on wards.

But we can implement action bar in pre Honycomb versions upto API Level 7 using ActionBarCompat library available with Android support library revision 18.

In this article, we will see how to implement action bar in pre honeycomb versions using ActionBarCompat support library.

This application is developed in Eclipse 4.2.0 with ADT plugin 22.0.4


1. Create new Android application project

Application Name : ActionBarCompatDemo

Project Name : ActionBarCompatDemo

Package Name : in.wptrafficanalyzer.actionbarcompatdemo

Minimum Required SDK : API 8 : Android 2.2 ( Froyo )

Target SDK : API 17 : Android 4.2 ( Jelly Bean )


2. Setup ActionBarCompat library in Eclipse

Please refer the article titled “Android – Setting up ActionBarCompat support library in Eclipse


3. Add reference to ActionBarCompat library

Add reference to ActionBarCompat support library

Figure 1 : Add reference to ActionBarCompat support library


4. Update the file AndroidManifest.xml

Note : As shown in the highlighted line 15,  the activity that implements the action bar should apply the style Theme.AppCompat or Theme.AppCompat.Light or Theme.AppCompat.DarkActionBar


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.wptrafficanalyzer.actionbarcompatdemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.AppCompat" >
        <activity
            android:name="in.wptrafficanalyzer.actionbarcompatdemo.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>


5. Update the class MainActivity in the file src/in/wptrafficanalyzer/actionbarcompatdemo/MainActivity.java

Note : As shown in the highlighted line7, the activity should extend the class ActionBarActivity.


package in.wptrafficanalyzer.actionbarcompatdemo;

import android.os.Bundle;
import android.view.Menu;

import android.support.v7.app.ActionBarActivity;
public class MainActivity extends ActionBarActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @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;
    }
}


6. Screenshot of the application

Android application with ActionBar is shown

Figure 2 : Android application with ActionBar is shown


7. Download Source code


How to hire me?

I am George Mathew, working as software architect and Android app developer at wptrafficanalyzer.in

You can hire me on hourly basis or on project basis for Android applications development.

For hiring me, please mail your requirements to info@wptrafficanalyzer.in.

My other blogs
store4js.blogspot.com


Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :



Tags: ,

5 Responses to Implementing Action Bar using ActionBarCompat support library in Android

  1. shashidhar on August 1, 2013 at 9:21 pm

    hi in manifest file Theme.Appcompat not exits?

    • george on August 1, 2013 at 9:41 pm

      Hi Shashidhar,

      It is already there in the article as well as in the attached source code.

  2. Aslan on February 1, 2014 at 11:02 pm

    Hi, I have downloaded and ran this app, all works fine. Then I added this line after setContentView(), getSupportActionBar().setTitle(“Title”); but it throws me NullPointerException. why?

  3. rajaa on April 10, 2014 at 4:19 pm

    can we use this method for min sdk 7 ?

  4. qwer on April 13, 2014 at 9:28 pm

    “the activity that implements the action bar should apply the style Theme.AppCompat or..”

    Thanks, that helped me out :)

Leave a Reply

Your email address will not be published. Required fields are marked *

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner