Code2care : TechZone Apps & Tutorials

ActionBarSherlock Implementation to Support Older Android SDK



ActionBarSherlock lets you implement Action Bars for older devices (Older SDK versions). I was not able to find any good straight forward way on how to implement it, so here is a tutorial on how i managed to implement it for my applications.
ActionBarSherlock Implementation for your Android App Demo


Implementation is pretty simple for having Menu's

1. Import the ActionBarSherlock library in your workspace. Make sure that you have the Sherlock files in the same drive as of your project files ( if working on a Windows machine e.g. D:/ drive) or else you may get linking error!
Import Actionbarsherlock library into your android workspace

2. Link ActionBarSherlock library to your Project.
Add ActionBarSherlock library to your android Project

3. Now you need to set the Sherlock theme to your Application Bar. There are some Dark and Light theme options available to choose from. I have set Theme.Sherlock.Light.DarkActionBar theme. The Theme has to be set in AndroidManifest.xml file, just replace the android:theme value in your application tag.

<application
  android:allowBackup="true"
  android:icon="@drawable/ic_launcher"
  android:label="@string/app_name"
  android:theme="@style/Theme.Sherlock.Light.DarkActionBar" >
  <activity
   android:name="com.code2care.actionbarsherlockeg.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>


5. Create Menu's under Drawable folder. You can populate each items with Images, place the images in drawable folders. You can have multiple Drop down menus implementations too. We load these menu in onCreateOptionsMenu function.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/message"
        android:icon="@drawable/mail32"
        android:showAsAction="never"
        android:title="Message"/>
    <item
        android:id="@+id/email"
        android:icon="@drawable/email"
        android:showAsAction="never"
        android:title="Email"/>
    <item
        android:id="@+id/others"
        android:showAsAction="never"
        android:title="More ..."/>

</menu>


4. Changes in MainActivity.java file.
  • Change "MainActivity extends Activity" to MainActivity extends SherlockActivity. Not that the have the correct imports.
    import com.actionbarsherlock.app.SherlockActivity;
    import com.actionbarsherlock.view.Menu;
    import com.actionbarsherlock.view.MenuItem;
    import com.actionbarsherlock.view.SubMenu;

  • Inflate these menu in onCreateOptionsMenu function.


    	@Override
    	public boolean onCreateOptionsMenu(Menu menu) {
    
    		SubMenu sub1 = menu.addSubMenu(R.string.share);
    		sub1.getItem().setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
    		sub1.getItem().setIcon(R.drawable.ic_action_share);
    
    		getSupportMenuInflater().inflate(R.menu.actionbar_share_menu, sub1);
    
    		SubMenu sub = menu.addSubMenu("More");
    		sub.getItem().setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
    		sub.getItem().setIcon(R.drawable.ic_action_overflow);
    
    		getSupportMenuInflater().inflate(R.menu.actionbar_context_menu, sub);
    
    		return true;
    	}
    


  • When an Item in the Sherlock Actionbar is selected , the action to be performed is written in onOptionsItemSelected function.


  • /*************** Options Menu Case *******************/
    public boolean onOptionsItemSelected(MenuItem item) {
    		
     //Implementing action on menu item being selected!
    		
       switch (item.getItemId()) {
    
       case 0:
       if ("share".equals((String) item.getTitleCondensed())) {
    
    	}
    	break;
    
    	case R.id.others:
    	Toast.makeText(getApplicationContext(),
    	"Others!", Toast.LENGTH_SHORT)
    	.show();
            break;
    
    	
    			
    	case R.id.rate:
    	Toast.makeText(getApplicationContext(),
    	"Rate!", Toast.LENGTH_SHORT)
    	.show();
    	break;	
    
    
            case R.id.message:
    	Toast.makeText(getApplicationContext(),
    	"Message!", Toast.LENGTH_SHORT)
    	.show();
    	break;
    
    	case R.id.email:
    	Toast.makeText(getApplicationContext(),
            "Email!", Toast.LENGTH_SHORT)
    	.show();
    	break;
    			
    	case R.id.feedback:
    	Toast.makeText(getApplicationContext(),
    	"Feedback!", Toast.LENGTH_SHORT)
    	.show();
    	break;
    
    	}
       return super.onOptionsItemSelected(item);
    }


      This is how it looks

      Thats it!, So very simple.

      GitHub link :

      ActionBarSherlock Demo








      Top




    Posts related to ActionBarSherlock.


    1. Actionbar Sherlock IllegalStateException Exception

    2. ActionBarSherlock Implementation to Support Older Android SDK


    Posts related to android.


    1. Share image and text Twitter using your Android Application Programatically

    2. The Android Virtual Device 'myEmulator' is currently running an emulator and cannot be deleted.

    3. Make Android View Scrollable both Horizontally and Vertically

    4. Can we move apps like WhatsApp, Facebook to external MicroSD card

    5. Parsing Data for android-21 failed unsupported major.minor version 51.0

    6. Android : Prevent App for rotation landscape or portrait

    7. ADT quit unexpectedly error on Mac OSX Android Eclipse SDK

    8. Android-Failed to install apk on device EOF Timeout Error

    9. How to change Android EditText Cursor Color

    10. JavaDocs for Android SDK Eclipse IDE

    11. Two Buttons next to each other in Android Layout

    12. Change Title text for Android Activity using java code

    13. How to add Newline to text in Android TextView

    14. Android : Unable to load VM from snapshot : Mac OS X Error

    15. INVALID FILE NAME: MUST CONTAIN ONLY [a-z0-9_.] Android Eclipse Error

    16. Hide Tittle bar and Navigation bar from android activity

    17. appcompat_v7 errors after updates to API level 21 Material Theme

    18. How to Copy Text to Android Clipboard Programatically ClipboardManager (Both Old and New SDK)

    19. [Solution] Running Android Lint has encountered a problem NullPointerException Error

    20. Remove Trailing zeros BigDecimal Java

        more...

    Posts related to github.


    1. GitHub website not working India (blocked)

    2. Android Simple Swipe detection template code on github

    3. ActionBarSherlock Implementation to Support Older Android SDK


    Code2care : Lines of Code for Change
    Code2care is an initiative to publish and share varied knowledge in technical and non-technical areas gathered during day-to-day learnings and development activities.





    Android app on Google Play    








    Popular tags
    android
    x 175
    eclipse
    x 29
    notepadplusplus
    x 20
    macosx
    x 14
    sharepoint
    x 14
    html
    x 14
    mac
    x 13
    androidstudio
    x 11




    1000+ C Programs     PHP Tutorial     JSON Tutorial     Swift Tutorial     India Pinocdes     About Us     Privacy Policy


    Code2care © 2012-17