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

package com.example.actionbar;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.SubMenu;

public class MainActivity extends SherlockActivity {

 private SubMenu mGoItem;

 private static final int GO_ITEM_ID = 1;

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

  ActionBar ab = getSupportActionBar();
  ab.setBackgroundDrawable(getApplicationContext().getResources()
    .getDrawable(R.drawable.bg_titlebar_tile));
  ab.setLogo(getResources().getDrawable(R.drawable.logo));
  ab.setDisplayShowTitleEnabled(false);
 }

 @SuppressLint("InlinedApi")
 @Override
 public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {

  mGoItem = menu.addSubMenu(0, GO_ITEM_ID, 0, null);
  mGoItem.setIcon(R.drawable.setting_icon);
  // SubMenu sub = menu.addSubMenu("Theme");
  mGoItem.add(0, R.style.Theme_Sherlock, 0, "View Profile");
  mGoItem.add(0, R.style.Theme_Sherlock_Light, 0, "Account Privacy");
  mGoItem.add(0, R.style.Theme_Sherlock_Light_DarkActionBar, 0,
    "Logout");
  mGoItem.getItem().setShowAsAction(
    MenuItem.SHOW_AS_ACTION_ALWAYS
      | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

  return true;
 }

 @SuppressLint("InlinedApi")
 @Override
 public boolean onOptionsItemSelected(
   com.actionbarsherlock.view.MenuItem item) {

  switch (item.getItemId()) {
  case GO_ITEM_ID:
   Toast.makeText(MainActivity.this,
     "You have Pressed 'Setting' Menu Button", Toast.LENGTH_LONG)
     .show();
   return true;
  }

  return false;
 }

}

2.activity_main.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textColor="@android:color/black"
        android:textStyle="bold"
        android:text="@string/hello_action_bar" />

</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.