. f ActionBarSherlock with custom View | Changing sherlock action bar menu item background ~ Android Developers Blog

Friday, 12 July 2013

ActionBarSherlock with custom View | Changing sherlock action bar menu item background

Hello Friends,
Today , I am going to share my another android tutorial which is based on the
Sherlock Action Bar. ABS(Action Bar Sherlock) is an support library which helps
to use action bar design pattern over all android devices.

Initially , Action bar is supportable over android version 3.0. But if you want to
provide the interactive user interface on lower version of device too, then
ABS comes in a role.

 
sherlock action barandroid action bar
       

1. MainActivity.java

  1. package com.example.actionbar;  
  2.   
  3. import android.annotation.SuppressLint;  
  4. import android.os.Bundle;  
  5. import android.view.MenuItem;  
  6. import android.widget.Toast;  
  7.   
  8. import com.actionbarsherlock.app.ActionBar;  
  9. import com.actionbarsherlock.app.SherlockActivity;  
  10. import com.actionbarsherlock.view.SubMenu;  
  11.   
  12. public class MainActivity extends SherlockActivity {  
  13.   
  14.  private SubMenu mGoItem;  
  15.   
  16.  private static final int GO_ITEM_ID = 1;  
  17.   
  18.  @Override  
  19.  protected void onCreate(Bundle savedInstanceState) {  
  20.   super.onCreate(savedInstanceState);  
  21.   setContentView(R.layout.activity_main);  
  22.   
  23.   ActionBar ab = getSupportActionBar();  
  24.   ab.setBackgroundDrawable(getApplicationContext().getResources()  
  25.     .getDrawable(R.drawable.bg_titlebar_tile));  
  26.   ab.setLogo(getResources().getDrawable(R.drawable.logo));  
  27.   ab.setDisplayShowTitleEnabled(false);  
  28.  }  
  29.   
  30.  @SuppressLint("InlinedApi")  
  31.  @Override  
  32.  public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {  
  33.   
  34.   mGoItem = menu.addSubMenu(0, GO_ITEM_ID, 0null);  
  35.   mGoItem.setIcon(R.drawable.setting_icon);  
  36.   // SubMenu sub = menu.addSubMenu("Theme");  
  37.   mGoItem.add(0, R.style.Theme_Sherlock, 0"View Profile");  
  38.   mGoItem.add(0, R.style.Theme_Sherlock_Light, 0"Account Privacy");  
  39.   mGoItem.add(0, R.style.Theme_Sherlock_Light_DarkActionBar, 0,  
  40.     "Logout");  
  41.   mGoItem.getItem().setShowAsAction(  
  42.     MenuItem.SHOW_AS_ACTION_ALWAYS  
  43.       | MenuItem.SHOW_AS_ACTION_WITH_TEXT);  
  44.   
  45.   return true;  
  46.  }  
  47.   
  48.  @SuppressLint("InlinedApi")  
  49.  @Override  
  50.  public boolean onOptionsItemSelected(  
  51.    com.actionbarsherlock.view.MenuItem item) {  
  52.   
  53.   switch (item.getItemId()) {  
  54.   case GO_ITEM_ID:  
  55.    Toast.makeText(MainActivity.this,  
  56.      "You have Pressed 'Setting' Menu Button", Toast.LENGTH_LONG)  
  57.      .show();  
  58.    return true;  
  59.   }  
  60.   
  61.   return false;  
  62.  }  
  63.   
  64. }  

2.activity_main.xml


  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="@android:color/white"  
  6.     tools:context=".MainActivity" >  
  7.   
  8.     <TextView  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_centerHorizontal="true"  
  12.         android:layout_centerVertical="true"  
  13.         android:textColor="@android:color/black"  
  14.         android:textStyle="bold"  
  15.         android:text="@string/hello_action_bar" />  
  16.   
  17. </RelativeLayout>  

Download the complete source code: Here

Mukesh Kumar

Hi Guys I am from Delhi working as Web/Mobile Application Developer(Android Developer), also have knowledge of Roboelctric and Mockito ,android test driven development... Blogging has been my passion and I think blogging is one of the powerful medium to share knowledge and ideas....

0 comments:

Post a Comment

 

Copyright @ 2013 Android Developers Blog.