Should use android:showAsAction when not using the appcompat library

22,161

Solution 1

You need to do three things-

  1. Add these 3 things xmlns:tools="schemas.android.com/tools" tools:context="com.example.sample.YourActivity" xmlns:yourapp="http://schemas.android.com/apk/res-auto" to your <menu> tag

    along with the usual xmlns:android="http://schemas.android.com/apk/res/android"

  2. Add yourapp:showAsAction="ifRoom" to your <item>.

  3. Finally, clean your project via Project -> Clean...

The last step is what solved it for me. Simply cleaning the project (they can get so dirty sometimes)

This is what the final code looks like:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="schemas.android.com/tools"
tools:context="com.example.sample.YourActivity"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >

<item
    android:id="@+id/action_share"
    android:orderInCategory="100"
    android:title="Share"
    android:icon="@drawable/ic_share_white"
    yourapp:showAsAction="ifRoom" />

</menu>

Solution 2

This works for me i have used appcompatlibrary:

compat:showAsAction="always"

instead of:

app:showAsAction="always"

And to use compat:showAsAction="always" , inside menu.xml include the line:

<menu   
   ......
  <!-- include the below line -->
    xmlns:compat="http://schemas.android.com/apk/res-auto" >

Solution 3

Faced the same problem. These two options worked for me:

1.Including - xmlns:compat="http://schemas.android.com/apk/res-auto" > and then using:"compat:showAsAction="ifRoom"/>"

2.Not including the compat and simply using: app:showAsAction="ifRoom"/>

PS: Using Android Studio.

Solution 4

Should use android:showAsAction when not using the appcompat library

Issue: Ensures that menu items are using the right namespace Id: AppCompatResource

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.

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

    <item
        android:id="@+id/action_refresh"
        android:orderInCategory="100"
        android:title="@string/action_refresh"
        android:icon="@drawable/toc"
        tools:ignore="AppCompatResource"
        compat:showAsAction="always"/> 
</menu>

Solution 5

All you need to do is add the following line to your item element to suppress the warning. Having the namespace as compat vs app for "http://schemas.android.com/apk/res-auto" doesn't actually do anything special.

tools:ignore="AppCompatResource"

full code:

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

    <item
        android:id="@+id/action_refresh"
        android:orderInCategory="100"
        android:title="@string/action_refresh"
        android:icon="@drawable/toc"
        tools:ignore="AppCompatResource"
        compat:showAsAction="always"/>
</menu>

Note: the compat tag could be app or whatever you want.

Share:
22,161
VahidMohammadian
Author by

VahidMohammadian

Updated on July 23, 2022

Comments

  • VahidMohammadian
    VahidMohammadian almost 2 years

    I have a weird problem, I was using a menu.xml file in my android application like this:

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:compat="http://schemas.android.com/apk/res-auto" >
    
        <item
            android:id="@+id/action_refresh"
            android:orderInCategory="100"
            android:title="@string/action_refresh"
            android:icon="@drawable/toc"
            compat:showAsAction="always"/>
    </menu>
    

    and it was working well, but some days ago i was updating my eclipse to the last version, and after that, the app:showAsAction="always" shows the error:

    Should use android:showAsAction when not using the appcompat library and after that my action bar icons was moved to the overflow bar and not showing the in the action bar at all.

    anyway, the appcompat library is the parent of my base style. what should I do to resolve that?