Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should use android:showAsAction when not using the appcompat library

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?

like image 795
VahidMohammadian Avatar asked Nov 12 '14 15:11

VahidMohammadian


3 Answers

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>
like image 166
Advait Saravade Avatar answered Nov 20 '22 20:11

Advait Saravade


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" >
like image 37
Mohammed Ali Avatar answered Nov 20 '22 20:11

Mohammed Ali


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.

like image 5
Santosh Pillai Avatar answered Nov 20 '22 20:11

Santosh Pillai