30

I'm using the new Action Bar Support and all my action views are shown in overflow and not as icons in the bar. My app is for 7+ API.

HomeActivity:

public class HomeActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle(R.string.app_name);
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeButtonEnabled(true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.home, menu);
        return true;
    }
}

home.xml

<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/action_settings"/>
<item
    android:id="@+id/action_browse"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/title_activity_browse"/>
<item
    android:id="@+id/action_search"
    android:actionViewClass="android.widget.SearchView"
    android:icon="@android:drawable/ic_menu_search"
    android:showAsAction="ifRoom|collapseActionView"
    android:title="@string/text_search"/>
<item
    android:id="@+id/action_scan"
    android:icon="@drawable/action_scan"
    android:showAsAction="always"
    android:title="@string/title_activity_browse"/>

I'm deploying on Nexus 7 with 4.3 and on LGP500 with 2.3.3 and no icons. I've also added android:theme="@style/Theme.AppCompat.Light.DarkActionBar" on the manifest and my project is correctly referencing android-support-v7-appcompat as described in the official doc.

Jumpa
  • 4,319
  • 11
  • 52
  • 100
  • 1
    possible duplicate of [android.support.v7 with \`ActionBarActivity\` no menu shows](http://stackoverflow.com/questions/17881547/android-support-v7-with-actionbaractivity-no-menu-shows) – BeccaP Oct 16 '13 at 23:56

2 Answers2

82

you have to define your menu resource files with also the attributes for the support library.

To implement the back support it reads them instead of the ones defined in older Android version.

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

<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:showAsAction="never"
    yourapp:showAsAction="never"
    android:title="@string/action_settings"/>
<item
    android:id="@+id/action_browse"
    android:orderInCategory="100"
    android:showAsAction="never"
    yourapp:showAsAction="never"
    android:title="@string/title_activity_browse"/>
<item
    android:id="@+id/action_search"
    android:actionViewClass="android.support.v7.widget.SearchView"
    android:icon="@android:drawable/ic_menu_search"
    android:showAsAction="ifRoom|collapseActionView"
    yourapp:showAsAction="ifRoom|collapseActionView"
    yourapp:actionViewClass="android.support.v7.widget.SearchView"
    android:title="@string/text_search"/>
<item
    android:id="@+id/action_scan"
    android:icon="@drawable/action_scan"
    android:showAsAction="always"
    yourapp:showAsAction="always"
    android:title="@string/title_activity_browse"/>

</menu>

NB remeber that for the SearchView class changed. it's now used the one from the support library so you also have to update your code in the onCreateOptionsMenu()

EDIT: here is a pretty good tutorial on how to migrate from ActionBarSherlok to AppCompat

Mario Lenci
  • 10,422
  • 5
  • 39
  • 50
  • Yeah, it works fine except for the search view: the icon is displayed correctly but nothing happens if a click on it...anyway I'm really having hard times getting the point here, is it a sort of default schema overriding? Why i didn't found any documentation about it? :( – Jumpa Aug 07 '13 at 09:38
  • yes, it is. Basically to be able to implement the back support they couldn't read the new menu attributes defined only after API 11 ( ? not sure ) so they defined new attributes and they use them. I'm gonna edit my answear with the correct searchView implamenetation. – Mario Lenci Aug 07 '13 at 09:40
  • K, marked as answered, can you say anything about search icon issue? EDIT: solved adding custom tag =) Many thanks again. – Jumpa Aug 07 '13 at 09:42
  • I've just noticed that search button works correctly but in logcat i get: "could not find class android.support.v7.widget.SearchView$5...". I don't understand, I've added yourapp:actionViewClass="android.support.v7.widget.SearchView" and that class is visible (i.e. via a normal import). – Jumpa Aug 07 '13 at 13:22
  • mh really?! sorry, i can't help you on this right away, that's probably happening also on my app. gonna check. – Mario Lenci Aug 07 '13 at 13:28
  • sry for the late answear. Are you getting that logcat on a 4.x device? – Mario Lenci Aug 28 '13 at 09:26
  • Exactly the same problem described here: http://stackoverflow.com/questions/18386739/could-not-find-class-android-support-v7-widget-searchview5 on 2.3.3. – Jumpa Aug 30 '13 at 10:05
  • 1
    it's just a guess, but it might be that if you change android:actionViewClass with android.widget.SearchView you won't get that error. But i think that then you'd have to manage the difference by code – Mario Lenci Aug 30 '13 at 15:48
  • @MarioLenci yourapp:actionViewClass="android.support.v7.widget.Spinner" not working !!!for drop down spinner any idea? – LOG_TAG Oct 15 '13 at 11:49
  • I'm not sure i understood the problem. anyway at first I'd check the code that initialize the Spinner in the onCreateOptionMenu(...) callback. Honestly i never had to use a spinner into the ActionBar, make sure the code looks like something like here ( http://stackoverflow.com/questions/8312344/how-to-add-a-dropdown-item-on-the-action-bar ) and then add the appcompat functionalities – Mario Lenci Oct 15 '13 at 15:15
  • Hey! I understand this is an old question, but I was curious if the double declaration for showAsAction is required? When using the support libraries my phone (4.3) doesn't seem to read the android: tagged one even when "yourapp" is missing. – schme May 30 '14 at 00:45
  • I'm not sure what you mean, anyway the correct way is to declare them both. – Mario Lenci May 30 '14 at 08:01
  • @MarioLenci Could you perhaps link to a resource that would explain why? Sorry if I'm incomprehensible. What I meant is: on my phone which I use for debugging (it uses Android 4.3) it didn't seam to make a difference if the android: namespaced one is declared or not, when using support libraries (hence the myapp namespaced one). I guess what I'm trying to get at is, when/where is the android.showAsAction used if both are declared. – schme May 30 '14 at 13:30
  • 2
    What is "yourapp" in `yourapp:actionViewClass`. Apparently it is not the package name because I tried 'com.mycompany.myproject` and got nothing. – Katedral Pillon Oct 01 '14 at 21:49
  • It's just an identifier for your custom xml.schema defined at the top of the layout. – Jumpa Jul 11 '15 at 19:30
  • Must both attributes be used at the same time? Those with android prefix AND those with yourapp prefix? – f470071 Nov 03 '15 at 13:28
  • 1
    The ones with yourapp prefix should be enought if you use the AppCompat MenuInflater – Mario Lenci Nov 04 '15 at 11:26
-1

A common mistake is forgetting to include the title string in your string.xml file. Make sure you include it for each menu item.

Suppose our menu xml contains the following item :

    <item
    android:id="@+id/toggle_action"
    android:orderInCategory="1"
    android:showAsAction="ifRoom"
    android:title="@string/toggle_action"/>

If we forget to include the value for the string toggle_action, then the action bar icon will not show.

Go to your strings.xml file and add in the following:

    <string name="toggle_action">TOGGLE</string>
Shonu93
  • 846
  • 1
  • 10
  • 19