0

I just got the pulldown navigation to work with ActionbarSherlock. Here is how it looks like:

enter image description here

And what I am wondering is whether I can put the sharing widget right next to the pulldown on the same action bar? If so, how can I do that? Is it part of the layout xml that I have to add? Or where/what needs to be specified?

This is my values/arrays.xml file

<resources>
    <string-array name="locations">
        <item>Home</item>
        <item>Learn</item>
        <item>Services</item>
        <item>Next Steps</item>
    </string-array>
</resources>

And this is how I start the activity:

private TextView mSelected;
private String[] mLocations;

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setTheme(R.style.Theme_Sherlock_Light);

But when I try to add both, the sharing and the list-navigation. It doesn't render one or the other. Here is what I try to do:

    Context context = getSupportActionBar().getThemedContext();
    ArrayAdapter<CharSequence> list = ArrayAdapter.createFromResource(
            context, R.array.locations, R.layout.sherlock_spinner_item);
    list.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);

    getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    getSupportActionBar().setListNavigationCallbacks(list, this); 

Thanks, Alex

RUNTIME ERROR:

java.lang.ClassCastException: android.widget.ShareActionProvider cannot be cast to com.actionbarsherlock.view.ActionProvider
        at com.actionbarsherlock.view.MenuInflater$MenuState.readItem(MenuInflater.java:389)
        at com.actionbarsherlock.view.MenuInflater.parseMenu(MenuInflater.java:162)
        at com.actionbarsherlock.view.MenuInflater.inflate(MenuInflater.java:112)
        at com.marketing.MainActivity.onCreateOptionsMenu(MainActivity.java:880)
        at com.actionbarsherlock.app.SherlockActivity.onCreatePanelMenu(SherlockActivity.java:184)
        at com.actionbarsherlock.ActionBarSherlock.callbackCreateOptionsMenu(ActionBarSherlock.java:559)
        at com.actionbarsherlock.internal.ActionBarSherlockNative.dispatchCreateOptionsMenu(ActionBarSherlockNative.java:65)
        at com.actionbarsherlock.app.SherlockActivity.onCreateOptionsMenu(SherlockActivity.java:149)
        at android.app.Activity.onCreatePanelMenu(Activity.java:2444)
        at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:388)
        at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:739)
        at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:2833)
        at android.os.Handler.handleCallback(Handler.java:605)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4424)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
        at dalvik.system.NativeStart.main(Native Method)
Genadinik
  • 18,153
  • 63
  • 185
  • 284

2 Answers2

3

You can read how to add ShareActionProvider to SherlockActionBar here /simple tutorial/

vsvydenko
  • 729
  • 1
  • 9
  • 22
  • the difficulty I am having is adding the ShareActionProvider and the ListNavigation at the same time. That is my issue. I can do them individually already. – Genadinik Jun 21 '13 at 14:09
2

Look here for information on how to add an easy share action widget. Yes, in your layout menu xml you would add it as another item. It looks as if there is little room for an icon, so you might want to push the current "Learn" menu into overflow.

Edit: I think it would be simpler to do this by inflating your entire menu from xml. Examples can be found here, but basically you override onCreateOptionsMenu:

public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.main_menu, menu);
    return true;
}

menu/main_menu.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android">  

<item android:id="@+id/menu_item_share"         
android:showAsAction="ifRoom"         
android:title="Share"         android:actionProviderClass="android.widget.ShareActionProvider" />  

<item
android:id="@+id/menuSort"
android:showAsAction="ifRoom"
android:actionLayout="@layout/action_sort"  />

</menu>

layout/action_sort.xml:

<Spinner xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:entries="@array/locations" />
jkau
  • 465
  • 4
  • 12
  • thank you. Would you be able to elaborate on how it would look to have the share widget inside the menu? Shouldn't it not be inside the menu? I am confused about how to add 2 SherlockActions in the tool bar at the same time. – Genadinik Jun 21 '13 at 14:10
  • yeah. I just added how I do that to my original question. – Genadinik Jun 21 '13 at 14:48
  • thank you, one error in there. What is the layout/action_sort in the next to last line of main_menu.xml ? It doesn't compile for me. – Genadinik Jun 21 '13 at 16:57
  • That layout will be the spinner that I put in at the end. Look at [this](http://stackoverflow.com/a/8312985/2426561) – jkau Jun 21 '13 at 17:58
  • ok now I have another error. I am going to update my question with it. – Genadinik Jun 21 '13 at 18:49
  • For MenuInflater, am I supposed to import com.actionbarsherlock.view.MenuInflater; or import android.view.MenuInflater; ? – Genadinik Jun 21 '13 at 18:53
  • If you are using ABS then you should use the ABS one – jkau Jun 21 '13 at 18:53
  • Also, realized since you are using ABS that it should be *getSupportMenuInflater()* instead of *getMenuInflater()* – jkau Jun 21 '13 at 18:54
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/32181/discussion-between-genadinik-and-jkau) – Genadinik Jun 21 '13 at 19:15
  • That got me a little further. Now getting a runtime exception which I am posting in the original question. – Genadinik Jun 21 '13 at 19:55
  • Also, could you join the chat? Maybe it could be much faster to figure this out - thank you for all your help so far by the way! – Genadinik Jun 21 '13 at 19:57