Bring ActionBar to pre-Honeycomb devices with ActionBarSherlock

    As Android developers have known, the native Action Bar (App Bar) is available from API 11 (Android 3.0). But what about bring it to lower API devices (Android 2.x)? We can use an external library to deal with this problem.
    ActionBarSherlock is a library by Jake Wharton, that enables you to use action bars even on older devices without having to code an action bar from scratch. ActionBarSherlock automatically uses the native action bar when appropriate or wrap a custom implementation around your layouts. Using ActionBarSherlock allows you to easily develop an application with an Action Bar for every version of Android from 2.x and up.
    In this tutorial, you will learn how to implement ActionBarSherlock into your Android application.

Import to Android Studio Project

    After creating a new Android Project (I chosed the min-sdk is 8), you must import in to your project.
    Step 1: Download this library from Github, extract it, you will see these folders:
   Step 2: Add this library as an Android module, in Android Studio menu, choose File --> New... --> Import Module... and link to the "actionbarsherlock" folder:
    Step 3: After above step, we have a new module named "actionbarsherlock" in project. Edit the build.gradle file of 'app' module and add this library dependency:
    After sync gradle, we have integrated ActionBarSherlock successful to our project.

Coding project

    The first important in code is define theme for project. ActionbarSherlock provide set of themes start with Theme.Sherlock prefix. For example:     Turn to Activity programmatically code, it must extends SherlockActivity, SherlockFragmentActivity,... (your Activity type with Sherlock prefix), depending on your purpose.
    For example:
import android.os.Bundle;

import com.actionbarsherlock.app.SherlockActivity;

public class MainActivity extends SherlockActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}
    And it default layout:
    Output when running app (in Android 2.3.3 device):

Styling theme for application

    Each ActionBarSherlock theme can be styled and customized like any Android SDK theme. Of course, adding some codes to styles.xml file to make your app look like better:
    Note: you can quick generate the Action Bar style with Android Action Bar Style Generator site.
    In the Activity code, I also provide more codes to create and display an Options Menu in ActionBar:
package info.devexchanges.actionbarsherlockexample;

import android.os.Bundle;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

public class MainActivity extends SherlockActivity {

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

    @Override
    public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
        getSupportMenuInflater().inflate(R.menu.menu_main, menu);

        //Bring a menu item to ActionBar in Android 2.3.3 device
        menu.findItem(R.id.about).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);

        switch(item.getItemId()){

            case R.id.computer:
                Toast.makeText(getBaseContext(), "You selected Computer", Toast.LENGTH_SHORT).show();
                break;

            case R.id.camera:
                Toast.makeText(getBaseContext(), "You selected Camera", Toast.LENGTH_SHORT).show();
                break;

            case R.id.email:
                Toast.makeText(getBaseContext(), "You selected EMail", Toast.LENGTH_SHORT).show();
                break;

        }
        return true;
    }
}
    And code for menu file (locate in res/menu folder):     When running app in 2.3.3 device, with the trick in onCreateOptionsMenu() method above, you will see only "About" label is shown in the ActionBar:
    Other Menu items will displayed when you press Menu button on your device:
    And after click any Menu item, a Toast will be shown:

ActionBarSherlock vs AppCompat library

    Finally, Google has released the v7 appcompat library. It adds support for the Action Bar user interface design pattern (to the lowest API 7). This library includes support for material design user interface implementations and works very well, bring the native Action Bar to 2.x devices, so ActionBarSherlock seem out-of-date. But, it still remains one of the most famous Android libraries, very helpful for developers in the previous period of time at Android programming work.

References


Share


Previous post
« Prev Post
Next post
Next Post »