elprl
elprl

Reputation: 1960

Custom pull-down from ActionBarSherlock Action Item

I'm creating a universal app compatible with Froyo upwards, so I'm using the brilliant ActionBarSherlock. I wish to create submenu pulldowns from Action Items in the ActionBar that includes icons and text in the rows. There are a few threads that ask similar questions but I got no where with trying to implement them. I've tried Spinners but I need API 8 compatibility, so then I tried IcsSpinner in the Sherlock lib but Jake had advised someone else not to rely on it in case the lib changed. I tried a custom ActionProvider to mimic the ShareActionProvider but I found it too complicated:

enter image description here This image shows exactly what I want but I could not get it to work with my app. My code is as follows:

public class AddDocActionProvider extends ActionProvider {
private Context mContext;

public AddDocActionProvider(Context context) {
    super(context);
    mContext = context;
}

@Override
public View onCreateActionView() {
    LayoutInflater layoutInflater = LayoutInflater.from(mContext);
    View view = layoutInflater.inflate(
            R.layout.actionbar_new_doc_action_provider, null);
    return view;
}

@Override
public boolean hasSubMenu() {
    return true;
}

@Override
public void onPrepareSubMenu(SubMenu subMenu) {

    // loop was here calling
    subMenu.add(0, id, 0, "Type 1")
                .setIcon(R.drawable.type_1)
                .setOnMenuItemClickListener(mOnMenuItemClickListener);
        // added type 2, 3, etc

}

@Override
public boolean onPerformDefaultAction() {
    // This is called if the host menu item placed in the overflow menu of the
    // action bar is clicked and the host activity did not handle the click.
    return true;
}

My SherlockFragmentActivity had this code:

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

    MenuItem newDoc = menu.add(0, MENU_ADD_DOC, 0, "New Document");     
    newDoc.setVisible(!isPhoneShowingStorageList);
    newDoc.setIcon(R.drawable.dark_content_new);
    newDoc.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
    mNewDocActionProvider = new AddDocActionProvider(getSupportActionBar().getThemedContext());
    newDoc.setActionProvider(mNewDocActionProvider);
}

I don't see the submenu and it also crashes on froyo phones when the actionbar invalidates.

Other threads I've looked at:

How to create a custom Pulldown in the Honeycomb ActionBar?

How to add a Dropdown item on the action bar

Custom drop-down from action item (actionbarsherlock)

While this isn't necessarily an ActionBarSherlock specific issue, frankly I can't believe something this easy should be so complicated to implement in the standard Action Bar. Any help would be greatly appreciated.

UPDATE: Using XML rather than code added the icons for me:

 <item
        android:id="@+id/menu_new_doc"
        android:icon="@drawable/dark_content_new"
        android:showAsAction="always"
        android:title="New Document">
        <menu>
            <item
                android:id="@+id/word2010"
                android:icon="@drawable/doc"
                android:title="Word 2010"/>
            <item
                android:id="@+id/excel2010"
                android:icon="@drawable/excel"
                android:title="Excel 2010"/>
        </menu>
    </item>

So in order to dynamically had the submenus, I had to do this:

MenuItem newDoc = menu.findItem(R.id.menu_new_doc);
    SubMenu subMenu = newDoc.getSubMenu();
    subMenu.clear();
    for (/* loop */) {
        MenuItem subMenuItem = subMenu.add(0, hash, 0, fileType.GetDescription());
            subMenuItem.setIcon(R.drawable.doc);            
    }

Upvotes: 1

Views: 4475

Answers (1)

BurunduK
BurunduK

Reputation: 293

i think you should set the icon to the newly added Item to the subMenu. Example :

public boolean onCreateOptionsMenu(Menu menu) {

SubMenu subMenu = menu.addSubMenu("Add");

subMenu.add("Add Subitem 1").setIcon(R.drawable.ic_action_add1);
subMenu.add("Add Subitem 2").setIcon(R.drawable.ic_action_add2);

MenuItem subMenu1Item = subMenu.getItem();
subMenu1Item.setIcon(R.drawable.ic_action_add);
subMenu1Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);


return super.onCreateOptionsMenu(menu);

}

Upvotes: 3

Related Questions