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.
This post is part of a series called Android TOP useful libraries
Import to Android Studio 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
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
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
References
- The official libary page on Github: https://github.com/JakeWharton/ActionBarSherlock
- Author home page: http://jakewharton.com/
- v7 appcompat library doc: http://developer.android.com/intl/ja/tools/support-library/features.html#v7-appcompat