I just got the pulldown navigation to work with ActionbarSherlock. Here is how it looks like:
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)
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" />