0

I created a small test app to practice setting up the action bar in a normal Activity (not ActionBarActivity), since I'm only planning on working with API level 15 and above. I want to display the search icon on the Action Bar. If I set up the menu_main.xml file like this, I can display the search icon, but not without an error:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  tools:context=".MainActivity">
<item android:id="@+id/action_search"
      android:icon="@drawable/ic_action_search"
      android:title="@string/action_search"
      android:showAsAction="ifRoom"/>
</menu>

I can get the app to run just fine in my emulator and show the icon, but an error pops while in Android Studio:

"When using the appcompat library, menu resources should refer to the showAsAction in the app: namespace, not the android: namespace. Similarly, when not using the appcompat library, you should be using the android:showAsAction attribute."

I can make the error go away by deleting the following line from the build.gradle file:

compile 'com.android.support:appcompat-v7:22.0.0'

However, it doesn't seem like a good idea to remove the support library (although I could be wrong here). If I use the app:namespace method like the error recommends, I can get rid of the error, but then the search icon does not show up on the action bar; instead, the search option appears in the overflow menu. How can I properly display the search icon without getting an error and without removing the support library? Do I just ignore the error? Should I just stick with using an ActionBarActivity for my apps (i.e., is there a reason to try to use a normal Activity with an Action Bar or am I wasting my time)? I'm using Android Studio for this project.

jhonkaman
  • 535
  • 1
  • 7
  • 14

3 Answers3

2

First, ensure that you are using app theme based on v7 library theme.

<resources>

    <!-- Application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light">

And you use ActionBarActivity. Appcompat is not just there for backward compatibility, but also bridges some gaps for the latest material UI.

In menu.xml file, You need to use the app namespace :

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      tools:ignore="AlwaysShowAction"
    >
    <item
        android:id="@+id/menu_search"
        android:menuCategory="system"
        android:icon="@drawable/ic_action_search"
        android:title="@string/search"
        app:showAsAction="always"
        />
</menu>

Notice the

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

and the

app:showAsAction="always" 

Its not using android: namespace but the v7 library resources namespace.

Also, android:showAsAction="ifRoom" does not guarantee android will show the icon always.

S.D.
  • 29,290
  • 3
  • 79
  • 130
  • When I do that the icon doesn't appear; instead, the search option shows up in the overflow menu. To clarify, the app will run on the emulator with the android namespace, but that particular line gets underlined in red in the editor as if there's an error. Deleting the support library removes the error, but other posts on this site seem to recommend keeping the library, so I'd rather not have to do that. – jhonkaman Apr 12 '15 at 18:43
  • @jhonkaman Did you set the app theme based on v7 library ? http://stackoverflow.com/a/17440170/1531054 – S.D. Apr 12 '15 at 18:46
  • Within my styles.xml file the parent attribute is set to parent="android:Theme.Holo.Light" – jhonkaman Apr 12 '15 at 18:48
  • Setting it to "Theme.AppCompat.Light" or even "Theme.AppCompat.Light.DarkActionBar" gets rid of the Action Bar entirely for some reason. I think it's because it's just a normal Activity and not an ActionBarActivity. – jhonkaman Apr 12 '15 at 18:59
  • @jhonkaman `ActionbarActivity` + `Appcompat.Theme` + v7 library. You need all three to work. Or remove v7 library all together. – S.D. Apr 12 '15 at 19:01
  • Ok, thanks. Is it always going to be that way then if I want to have an Action Bar? For some reason I was under the impression that since my MinSdkVersion was 15 I could just use a normal Activity with one of the Holo themes and it would work just fine. And this [post](http://stackoverflow.com/questions/22740300/is-there-any-reason-to-use-the-support-v4-library-in-android/22740525#22740525) makes it seem like I shouldn't remove the library. – jhonkaman Apr 12 '15 at 19:08
  • @jhonkaman IF you use v7 compat library, then yes, you need to use themes and stuff to fully integrate the library. Or you can just not use the v7 library at all. API 15 does has its normal action bar etc. – S.D. Apr 12 '15 at 19:40
0

Try changing

android:showAsAction="ifRoom" 

to

app:showAsAction="ifRoom"
Sanj
  • 850
  • 7
  • 7
  • When I do that the icon doesn't appear; instead, the search option shows up in the overflow menu. Also, according to the [documentation](http://developer.android.com/training/basics/actionbar/adding-buttons.html), the app namespace is only for compatibility with versions as low as Android 2.1. However, my minimum SDK is Android 4.0.3, so I feel like I should be able to use the android namespace. – jhonkaman Apr 12 '15 at 18:31
  • have you got other icons being added in onCreateOptionsMenu()? Does it still appear in overflow when you set to always. i.e. app:showAsAction="always" – Sanj Apr 12 '15 at 18:47
  • There was a Settings option in the overflow menu but no other icons. Deleting the settings item from the menu_main.xml file did not fix it. Setting it to app"showAsAction="always" does not fix it either. – jhonkaman Apr 12 '15 at 18:52
  • Sorry not too sure whats going on here, always used it with ActionBarActivity. – Sanj Apr 12 '15 at 19:03
  • It seems like that is the right way to do it. I will probably just use ActionBarActivity from now on as well. – jhonkaman Apr 12 '15 at 19:09
0

Actually I faced the same issue three days ago, have been trying many solutions found online but didn't work until now. By following the preceded comments, all i did that makes it work out for me is pasted below, hope it worked.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
        app:showAsAction="always"
  xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- Search / will display always -->
<item android:id="@+id/action_search"
    android:icon="@drawable/ic_action_search"
    android:title="@string/action_search"
    app:showAsAction="always"/>
</menu>

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    </style>

</resources>