Search code examples
androidactionbarsherlock

ActionBarsharlock: from submenu move from menu to another activity


I am using ActionBarSharlock in my Application for Action Bar. In actionBar I take a button and If I click the button I see a menu with three items. Here is the code-

public boolean onCreateOptionsMenu(Menu menu) {

        SubMenu subMenu1 = menu.addSubMenu("Action Item");
        subMenu1.add("Sample");
        subMenu1.add("Menu");
        subMenu1.add("Items");

        MenuItem subMenu1Item = subMenu1.getItem();
        subMenu1Item.setIcon(R.drawable.sub_menu_icon);
        subMenu1Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

        return super.onCreateOptionsMenu(menu);
    }

But I don't know how to make this three submenu1 item clickable and move one activity to another. What kind of function I need to write. Let me know It will be great help


Solution

  • First: why don't you add your menu items in the XML menu file in res/menu/your_file.xml:

    <menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/action_search"
          android:icon="@drawable/ic_action_search"
          android:title="@string/action_search"/>
    <item android:id="@+id/action_compose"
          android:icon="@drawable/ic_action_compose"
          android:title="@string/action_compose" />
    

    then inflate the menu in your activity:

    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu items for use in the action bar
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_activity_actions, menu);
    return super.onCreateOptionsMenu(menu);
    

    }

    and then override the onOptionsItemSelected method to handle the menu item clicks.

    Also Google developed their own library for ActionBar on API level < 11 so you should try using that one as well instead of Sherlock.

    Hope this helps. Best of luck.