An important window feature named Action Bar was introduced in Honeycomb (API Level 11) and post Honeycomb devices. It provides a dedicated space for identifying the application brand and user location, and a consistent navigation. However, Action Bar is not available for pre-Honeycomb devices. But ActionBarSherlock which is an extension of the compatibility library designed to facilitate the use of the action bar design pattern across all versions of Android with a single API. In short, what it does is help you implement Action Bar for all devices starting from Android 1.6. This tutorial will help you create Action Bar for Android 2.2. I have created a Progress Bar while downloading file from Internet and Menus.
Adding ActionBarSherlock Library to your New Project
Step 1. Download latest package of ActionBarSherlock. Create a new Android Project from existing source. The location of the source should be the library folder of ActionBarSherlock. Make sure you have unzipped ActionBarSherlock in your eclipse installation folder.

Create new Project from existing file system

Choose Build Target greater than API Level 14

Check Is Library box and click OK
Step 2. Create a new project in which you have to implement Action Bar with Build Target API greater than 14. Click on Project->Properties->Android->Click on Add near the Is Library and add the previous library you created.

Add ActionBarSherlock as library in your project
Step3. In every Activity, you have to extend the class with SherlockActivity not Activity. The following code in the main activity creates menu items in the action bar. It is similar to previous post on creating menus. All we have to do is change the imports.
Note: Make sure to include styles.xml and AndroidManifest.xml given at the end of this post before running the project.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
package com.cw.sherlock; import com.actionbarsherlock.app.SherlockActivity; import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.MenuItem; import android.os.Bundle; import android.widget.Toast; public class SherlockDemoActivity extends SherlockActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } @Override public boolean onCreateOptionsMenu(Menu menu) { getSupportMenuInflater().inflate(R.menu.menu, menu);; return super.onCreateOptionsMenu(menu); } public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.About: Toast.makeText(SherlockDemoActivity.this, "Android Menu example", Toast.LENGTH_SHORT).show(); return true; case R.id.Submenu: Toast.makeText(SherlockDemoActivity.this, "Submenu is Selected", Toast.LENGTH_SHORT).show(); return true; case R.id.Settings: Toast.makeText(SherlockDemoActivity.this, "Basic Settings is Selected", Toast.LENGTH_SHORT).show(); return true; case R.id.submenu_1: Toast.makeText(SherlockDemoActivity.this, "Sub Menu is Selected", Toast.LENGTH_SHORT).show(); return true; default: return super.onOptionsItemSelected(item); } } } |
Update the main activity with the following code. The following snippet is an imitation of previous post Android Download File Progress Bar.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
package com.cw.sherlock; import java.io.BufferedInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.net.URL; import java.net.URLConnection; import com.actionbarsherlock.app.SherlockActivity; import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.MenuItem; import com.actionbarsherlock.view.Window; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Toast; public class SherlockDemoActivity extends SherlockActivity { private int mProgress = 100; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //This has to be called before setContentView and you must use the //class in com.actionbarsherlock.view and NOT android.view requestWindowFeature(Window.FEATURE_PROGRESS); setContentView(R.layout.main); setSupportProgressBarVisibility(true); findViewById(R.id.download).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { if (mProgress == 100) { mProgress = 0; String url = "https://dl.dropbox.com/u/50249620/Surface%20by%20Microsoft.mp3"; new DownloadFile().execute(url); } } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getSupportMenuInflater().inflate(R.menu.menu, menu);; return super.onCreateOptionsMenu(menu); } public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.About: Toast.makeText(SherlockDemoActivity.this, "Android Menu example", Toast.LENGTH_SHORT).show(); return true; case R.id.Submenu: Toast.makeText(SherlockDemoActivity.this, "Submenu is Selected", Toast.LENGTH_SHORT).show(); return true; case R.id.Settings: Toast.makeText(SherlockDemoActivity.this, "Basic Settings is Selected", Toast.LENGTH_SHORT).show(); return true; case R.id.submenu_1: Toast.makeText(SherlockDemoActivity.this, "Sub Menu is Selected", Toast.LENGTH_SHORT).show(); return true; default: return super.onOptionsItemSelected(item); } } class DownloadFile extends AsyncTask<String, String, String> { @Override protected void onPreExecute() { super.onPreExecute(); Toast.makeText(getApplicationContext(), "Starting", Toast.LENGTH_SHORT).show(); } @Override protected String doInBackground(String... args) { int count; try { URL url = new URL(args[0]); URLConnection conection = url.openConnection(); conection.connect(); // getting file length int fileLength = conection.getContentLength(); InputStream is = new BufferedInputStream(url.openStream(), 8192); OutputStream os = new FileOutputStream("/sdcard/surface.mp3"); byte data[] = new byte[1024]; long total = 0; while ((count = is.read(data)) != -1) { total += count; publishProgress(""+(int)((total*100)/fileLength)); // writing data to file os.write(data, 0, count); } // flush output os.flush(); os.close(); is.close(); } catch (Exception e) { Log.e("Error: ", e.getMessage()); } return null; } //updating progress bar protected void onProgressUpdate(String... progress) { //progress percentage int mProgress = ((Window.PROGRESS_END - Window.PROGRESS_START) / 100) * (Integer.parseInt(progress[0])); setSupportProgress(mProgress); } @Override protected void onPostExecute(String file_url) { //toast to notify user of download completion Toast.makeText(getApplicationContext(), "File Downloaded and saved to sdcard", Toast.LENGTH_SHORT).show(); } } } |
layout->main.xml
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:gravity="center_horizontal" android:padding="20dip"> <Button android:id="@+id/download" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="40dp" android:paddingRight="40dp" android:text="Download" /> </LinearLayout> |
res->menu->menu.xml
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <!-- ifRoom attribute checks the space available in the action bar. If space is available, the menu is placed on the Action Bar. --> <item android:id="@+id/About" android:title="About" android:showAsAction="ifRoom" /> <item android:id="@+id/Submenu" android:title="Submenu >" android:showAsAction="ifRoom"> <menu> <item android:id="@+id/submenu_1" android:title="Sub Menu 1" /> </menu></item> <item android:id="@+id/Settings" android:title="Basic Settings" android:showAsAction="ifRoom" /> </menu> |
values->styles.xml
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?xml version="1.0" encoding="utf-8"?> <resources> <!-- Variation on the Holo Light theme that styles the Action Bar --> <style name="Theme.AndroidDevelopers" parent="Theme.Sherlock.ForceOverflow"> <item name="android:actionDropDownStyle">@style/MyDropDownNav</item> <item name="actionDropDownStyle">@style/MyDropDownNav</item> </style> <!-- style the list navigation --> <style name="MyDropDownNav" parent="Widget.Sherlock.Light.Spinner.DropDown.ActionBar"> </style> </resources> |
AndroidManifest.xml
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.cw.sherlock" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:targetSdkVersion="15" android:minSdkVersion="8" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/Theme.AndroidDevelopers" > <activity android:label="@string/app_name" android:name=".SherlockDemoActivity" > <intent-filter > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <!-- Internet Permission --> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <!-- Permission: Writing to SDCard --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> </manifest> |
Run the Project on a Android 2.2 Device. You can see the action bar working on all devices. Below are screenshot of the final app.

Menu in Action Bar

You can see the Progress Bar in the title bar.
I will continue to post more tutorials on implementation of ActionBarSherlock and other bakcward compatibility issues. Stay tuned.







