How to work in ActionBar support Library.

By | November 9, 2013

Hi all

Today’s post is all about working with ActionBar support libraries from Google.
These libraries support from Android 2.1 (API 7).

ActionBar Support Custom

For working with these libraries we have to follow some instructins.

Please follow these steps exactly for setting up a Actionbar support library.

1. Start the Android SDK Manager.
In the SDK Manager window, scroll to the end of the Packages list, find the Extras folder and, if necessary, expand to show its contents.
2. Select the Android Support Library item.
Note: If you’re developing with Android Studio, select and install the Android Support Repository item instead.
3. Click the Install packages… button.

ActionBar Support Installation

After downloading, the tool installs the Support Library files to your existing Android SDK directory. The library files are located in the following subdirectory of your SDK: /extras/android/support/ directory.

Now Open Eclipse and do the following.

Create a library project based on the support library code:

Make sure you have downloaded the Android Support Library using the SDK Manager.
Create a library project and ensure the required JAR files are included in the project’s build path:
Select File > Import.
Select Existing Android Code Into Workspace and click Next.

Browse to the SDK installation directory and then to the Support Library folder. For example, if you are adding the appcompat project, browse to /extras/android/support/v7/appcompat/.

Click Finish to import the project. For the v7 appcompat project, you should now see a new project titled android-support-v7-appcompat.

In the new library project, expand the libs/ folder, right-click each .jar file and select Build Path > Add to Build Path. For example, when creating the the v7 appcompat project, add both the android-support-v4.jar and android-support-v7-appcompat.jar files to the build path.

Right-click the project and select Build Path > Configure Build Path.
In the Order and Export tab, check the .jar files you just added to the build path, so they are available to projects that depend on this library project. For example, the appcompat project requires you to export both the android-support-v4.jar and android-support-v7-appcompat.jar files.
Uncheck Android Dependencies.

Click OK to complete the changes.

AndroidManifest.xml changes

Important
1. To work with support libraries of actionbar you should also have to change the theme to “Theme.AppCompat”.
2. Also your activity should extend ActionBarActivity.

This is all that needed.
After that you can add this actionbar compat library for any number of projects.

Usage.

Instead of using getActionBar(), use should use getSupportActionBar() to point to suppport actionbar libraries.

A sample looks like this.

package com.coderzheaven.actionbartest;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;

import com.example.actionbartest.R;

public class MainActivity extends ActionBarActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		getSupportActionBar().setTitle("ActionBar Support Demo");
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}

For that you have to make changes in the styles.xml.

This is my styles.xml in which I have made my ActionBar Custom.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- the theme applied to the application or activity -->
    <style name="CustomActionBarTheme"
           parent="@style/Theme.AppCompat.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/MyActionBar</item>

        <!-- Support library compatibility -->
        <item name="actionBarStyle">@style/MyActionBar</item>
    </style>

    <!-- ActionBar styles -->
    <style name="MyActionBar"
           parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="android:background">@drawable/blue</item>

        <!-- Support library compatibility -->
        <item name="background">@drawable/blue</item>
    </style>
</resources>

My strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">ActionBarTest</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    
    <drawable name="blue">#0000FF</drawable>

</resources>

So you are done.
Go on and run the project.

You can download a sample of Project which contains the ActionBarCompat Library and a sample project which links this library from here.
So when you import it to your eclipse make sure you import two projects included.

Website Pin Facebook Twitter Myspace Friendfeed Technorati del.icio.us Digg Google StumbleUpon Premium Responsive

Leave a Reply

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