Reputation: 241
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?
Upvotes: 17
Views: 22899
Reputation: 41
For me woks this approach:
<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">
<item
android:id="@+id/maker_appbar_next"
android:title="@string/next"
app:showAsAction="always"
tools:ignore="AppCompatResource" />
</menu>
Upvotes: 0
Reputation: 1966
I had the same issue in my multi-module Gradle project. It was reported by Android Lint CLI.
My modules look like:
feature_home
is an AppCompat
module, which has a menu like:
<?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">
<item
android:id="@+id/edit"
android:icon="@drawable/ic_edit"
android:title="@string/edit"
app:showAsAction="always" />
</menu>
I wanted to run Android Lint for the entire application with a single ./gradlew
command, so I added the following lint option to app/build.gradle
:
lintOptions {
checkDependencies true
}
Then, when I run ./gradlew :feature_home:lint
, everything was fine, but when I tried to check all modules via ./gradlew :app:lint
, I had the subject issue: Error: Should use android:showAsAction when not using the appcompat library
.
The problem was due to my app
module wasn't AppCompat
. So adding the AppCompat
dependency to app/build.gradle
resolved the issue:
implementation "androidx.appcompat:appcompat:$appcompat_version"
Upvotes: 1
Reputation: 374
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.
Upvotes: 1
Reputation: 356
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>
Upvotes: 2
Reputation: 3103
You need to do three things-
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"
Add yourapp:showAsAction="ifRoom"
to your <item>
.
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>
Upvotes: 19
Reputation: 8653
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.
Upvotes: 5
Reputation: 2926
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" >
Upvotes: 15