Reputation: 555
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.
Upvotes: 0
Views: 2465
Reputation: 37
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>
Upvotes: 0
Reputation: 29436
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.
Upvotes: 2
Reputation: 850
Try changing
android:showAsAction="ifRoom"
to
app:showAsAction="ifRoom"
Upvotes: 0