Android compatibility contextual action bar
Asked Answered
G

6

25

In trying to follow the Android Design Guidelines, I'm running into a small quandary.

I want to have a list of items that I can long-press several of (multi-select), and then perform bulk actions on them.

The Design Guidelines suggest using the Contextual Action Bar for this, and it sounds perfectly like what I had in mind. Problem is, I'm trying to maintain compatibility backwards to API 7 (due to my phone being 2.3.3 currently).

I'm using ActionBarSherlock to get other actionbar stuff, but I can't seem to figure out how to get it to either fire up a contextual action bar, nor have I figured out how to add buttons arbitrarily to the ActionBar in ABS. I see you can do tabs, so maybe that's the answer, but since I'm trying to allow multi-select, I don't want to have the normal modal context menu.

Ghibelline answered 2/3, 2012 at 18:0 Comment(3)
Hi did you check the sample source code, there are also samples of adding buttons, contextual buttons ..etc github.com/JakeWharton/ActionBarSherlock/tree/master/samples/…Distal
Yeah, I looked at the classes in that folder, I did not see the examples you're talking about. The only examples I saw of adding things are done in the OptionsMenu creation step, but I'm looking to do it in response to a long click on an item (logically a contextual thing). Can you tell me specifically what file you saw it in?Ghibelline
That's how I made it work #14738019Spondylitis
M
16

Setting up contextual actionbar is the same to setting up the 'regular' ActionBar items as far as the XML is concerned. This example in the developer's guide explains it all.

In order to use ActionBarSherlock, replace the default Android-callbacks to the ActionBarSherlock-edited callbacks (e.g. instead of Android.View.ActionMode, use com.actionbarsherlock.view.ActionMode).

Merchandising answered 13/3, 2012 at 19:40 Comment(3)
that namespace doesn't exist, as far as I can tell.Ghibelline
Are you using v4.0 of ActionBarSherlock? It got pulled out of beta last week.Merchandising
Upgrading to 4.0 will add the namespaces you're missing.Merchandising
U
22

This is a late answer, but I think would help people stuck.

Opening the contextual action bar is actually pretty simple, at any point in your activity you just have to call:

startActionMode(mActionModeCallback);

If you are not in your main activity, like in fragments, you can get a reference with

getSherlockActivity().startActionMode(mActionModeCallback);

and this is the callback

private ActionMode.Callback mActionModeCallback = new ActionMode.Callback(){

    @Override 
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
          MenuInflater inflater = mode.getMenuInflater();
          inflater.inflate(R.menu.actionbar_context_menu, menu);
          return true;
        }

    @Override
    public void onDestroyActionMode(ActionMode mode) {

    }

    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_item1:
                return true;
            case R.id.menu_item2:
                //close the action mode
                //mode.finish();
                return true;
            default:
                mode.finish();
                return false;
       }
    }
};

The xml is a simple menu like the actionbar one:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item android:id="@+id/menu_item1"
      android:icon="@drawable/ic_item1"
      android:title="@string/ITEM1"
      android:showAsAction="always|withText" />

<item android:id="@+id/menu_item2"
      android:icon="@drawable/ic_item2"
      android:title="@string/ITEM2"
      android:showAsAction="always|withText" />

Unblinking answered 20/9, 2012 at 7:34 Comment(1)
Wow nice awnser, have truggled with this for a couple of hours! great workEvangelize
M
16

Setting up contextual actionbar is the same to setting up the 'regular' ActionBar items as far as the XML is concerned. This example in the developer's guide explains it all.

In order to use ActionBarSherlock, replace the default Android-callbacks to the ActionBarSherlock-edited callbacks (e.g. instead of Android.View.ActionMode, use com.actionbarsherlock.view.ActionMode).

Merchandising answered 13/3, 2012 at 19:40 Comment(3)
that namespace doesn't exist, as far as I can tell.Ghibelline
Are you using v4.0 of ActionBarSherlock? It got pulled out of beta last week.Merchandising
Upgrading to 4.0 will add the namespaces you're missing.Merchandising
H
2

ActionBarSherlock has its own implementation of ActionMode, but you'll have to manualy controll its lifesycle, I wrote a tutorial about this.

Helban answered 6/5, 2013 at 9:52 Comment(0)
D
1

For long click sample please refer to below links. First one is java code required for sample. And second one is how to define the layout;

Distal answered 3/3, 2012 at 20:10 Comment(1)
That has nothing to do with my question. That example is how to show a context menu from a fragment. I'm trying to replicate the Contextual Action Bar from the more current APIs.Ghibelline
R
0

I will answer second part of your question. Here is an example how to add any View instance (button in the code below) actionbar with ActionBarSherlock library:

@Override
public boolean onCreateOptionsMenu(Menu menu) {

        refreshButton = (RotatingButton) LayoutInflater.from(this).inflate(R.layout.actionbar_customview_refresh, null);
        refreshButton.setOnClickListener(refreshButtonListener);

        MenuItem item = menu.add(0, android.R.id.copy, 0, getString(R.string.actionbar_refresh));
        item.setActionView(refreshButton);
        item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_activity_action_bar, menu);
}
Relevance answered 3/3, 2012 at 21:53 Comment(1)
Yes, I have done that before, but that makes it show on the actionbar all the time for that activity. What I'm trying to do is show the button only after items are selected in the list, and update after each item is selected.Ghibelline
G
0

I was facing the same issue. It was solved when I found this link. Basically, you have to create a callback class that implements ActionMode.Callback. In this class, you inflate the Action Bar with your contextual Action Bar. At each selection (or long click), you start the callback using the startActionMode method. See the link for an working code =]

EDIT: There is also an example on Sherlock's samples under /samples/demos/src/com/actionbarsherlock/sample/demos/ActionModes.java

Gonfalon answered 22/3, 2012 at 14:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.