Search code examples
androidandroid-actionbarandroid-actionbar-compat

AppCompat library doesn't seem to be working normally for me


I've been following this documentation to start using ActionBarCompat. I think I've done everything right but it just isn't working as it should. I'm trying to add a refresh button to action bar but instead of looking like an independent icon, it goes in the overflow menu.

Here's how it looks:

enter image description here

res/menu/activity.xml

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

<item 
    android:id="@+id/action_refresh"
    android:title="@string/action_refresh"
    android:icon="@drawable/ic_action_refresh"
    android:actionProviderClass="android.support.v7.widget.ShareActionProvider"
    android:showAsAction="ifRoom"/>

<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/action_settings"/>

onCreateOptionsMenu

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.schedule, menu);

    MenuItem refreshItem = menu.findItem(R.id.action_refresh);

    ShareActionProvider mActionProvider = (ShareActionProvider)
            MenuItemCompat.getActionProvider(refreshItem);

    //TODO FIX
    return super.onCreateOptionsMenu(menu);
}

startSupportActionMode (empty for now)

startSupportActionMode(new ActionMode.Callback() {

        @Override
        public boolean onPrepareActionMode(ActionMode arg0, Menu arg1) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public void onDestroyActionMode(ActionMode arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public boolean onCreateActionMode(ActionMode arg0, Menu arg1) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean onActionItemClicked(ActionMode arg0, MenuItem arg1) {
            // TODO Auto-generated method stub
            return false;
        }
    });

I know I probably did it completely wrong, but all I want to do is to haev a refresh button in my actionbar.


Solution

  • Change your menu's xml to this:

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto" >
    
        <item 
            android:id="@+id/action_refresh"
            android:title="@string/action_refresh"
            android:icon="@drawable/ic_action_refresh"
            android:actionProviderClass="android.support.v7.widget.ShareActionProvider"
            app:showAsAction="ifRoom"/>
    
        <item
            android:id="@+id/action_settings"
            android:orderInCategory="100"
            android:showAsAction="never"
            app:title="@string/action_settings"/>
    
    </menu>
    

    You were using android:showAsAction="ifRoom", but you have to use the showAsAction attribute with the custom namespace, which is app in your case, so the Compat ActionBar can use it.