Android Studio 0.5.8
Hello,
For some reason the icon never displays on the ActionBar, I have used a combination of ifRoom|withText but still doesn't display. I have also tried rotating in Landscape. I am using genymotion 4.4.2
<?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:title="#string/new_crime"
android:id="#+id/menu_item_new_crime"
android:icon="#drawable/ic_action_new"
app:showAsAction="always"/>
</menu>
I am inflating the menu in a fragment:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.fragment_crime_list, menu);
}
Here is a screenshot:
I have tried hardware nexus5 in portrait and landscape mode, but no icon.
I have also tried using the following, but didn't work either:
android:icon="#android:drawable/ic_menu_add"
Many thanks for any suggestions,
I have come across this issue once myself. Try this:
<?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:title="#string/new_crime"
android:id="#+id/menu_item_new_crime"
android:icon="#drawable/ic_action_new"
android:showAsAction="always"
app:showAsAction="always"/>
</menu>
I don't know why it would be necessary to have both, but that fixed it for me for some reason.
You are using Android Studio, as explained here by blackfizz: "the lint check sees that you have imported the appcompat library via gradle and it thinks that you should use the ActionBarActivity because of your library import. That's why you are getting the error."
I had the exact problem. Android Studio was giving me the error "should use app:showAsAction with the appcompat library with xmlns:app="schemas.android.com/apk/res-auto". If I changed my XML as suggested, my menus in the actionBar disappeared into the overflow. If I ignored the error, I got the expected behavior, but the error still bothered me.
The real culprits turned out to be the following lines in the file build.gradle:
dependencies {
…
compile 'com.android.support:appcompat-v7:22.1.1'
compile 'com.android.support:support-v4:22.1.1'
}
which imported the appcompat library, and caused all the trouble. Since I only targeted Android 4.4 and up, I was able to remove these two lines. Problem solved!
I wasted a few hours to figure it out myself before reading blackfizz's answer, so I am posting my answer here in hopes of saving other developers a few hours.
When you encounter a similar situation, first check your build.gradle to see if you have inadvertently imported the appcompat library.
You need to use Theme.Holo style and not AppCompat.
In order to do so, just change the style of the application at AndroidManifest.xml
If you get error:
should use app:showAsAction with the appcompat library with xmlns:app="schemas.android.com/apk/res-auto
Then you need to change the module settings:
1 - Right Click on your app and Select Open Module Settings (Or just Press F4)
2-In the depencencies, add a support module newer than V7 (for example com.android.support:support-v13:22.0.0)
in the menu.xml, dont write:
app:showAsAction="ifRoom"
but write
android:showAsAction="ifRoom"
Related
After creating a menu for my action bar. The little arrow doesn't appear next to the share icon and I'm not sure why this has happened or if it's intentional based on my code.
<?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/action_info"
android:title="#string/information"
android:icon="#android:drawable/ic_menu_info_details"
app:showAsAction="ifRoom"/>
<item android:id="#+id/action_share"
android:title="#string/menuitem_share"
android:icon="#android:drawable/ic_menu_share"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
app:showAsAction="ifRoom"/>
<item android:id="#+id/action_preferences"
android:title="#string/menuitem_preferences"
app:showAsAction="never"/>
</menu>
After viewing images of similar menus online, I noticed that little arrow (in this screenshot).
How can that little arrow be added to the share icon?
As far as I know, every new project you create in Android Studio uses the Material design (introduced with Android 5.0 Lollipop) with AppCompat themes. It is designed to show up on every compatible device thanks to Support Library (even on pre-Lollipop devices), and ActionBar menu items created when you are using Support Library looks like on the first image.
As far as I also know, I've seen this kind of arrowed Share button on the second image last time on a 4.x device. So I think you could re-create this kind of Share button if you would not use any theme and let your pre-Lollipop device to do the work with the ActionBar.
I've tried to recreate this behaviour by your code above, without luck. But then I opened the ApiDemos app which is on every Android Studio emulator image and shows a lot of functions built up in Android, for example ActionBar functions. There I saw that Share button looks differently on different API levels.
Android 4.4 KitKat:
Android 5.0 Lollipop:
I want my refresh item to appear on the top bar. I am extending ListActivity and using the theme android:Theme.Holo.Light.DarkActionBar. The menu is created using:
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="eu.pcas_project.client.android.pa.services.ServiceList">
<item
android:id="#+id/refresh_all_services"
android:title="#string/refresh_all_services"
android:icon="#drawable/ic_menu_refresh"
app:showAsAction="always" />
</menu>
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_service_list, menu);
return super.onCreateOptionsMenu(menu);
}
If I change app:showAsAction="always" on the menu XML to android:showAsAction="always", this works as I want it to—icon on top bar—but then I get an error saying Should use android:showAsAction when not using the appcompat library. Can it be fixed?
Targeting API 19.
It depends, if you hav a physical button for menu it doesn't appear. but you can try this:
<item
android:id="#+id/refresh_all_services"
android:icon="#drawable/ic_menu_refresh"
android:title="#string/refresh_all_services"
app:showAsAction="withText|ifRoom"
app:actionProviderClass="android.support.v7.widget.ActionProvider"
/>
As said in the question, android:showAsAction="always" works but generates an error.
While the project was not using the appcompat library directly, I discovered that a library imported by said project had com.android.support:appcompat-v7:XXX.YYY.ZZZ as a dependency even though it was not needed. This was added by Android Studio. Once the line was removed and the project rebuilt the error disappeared and the refresh icon appeared on the top right corner instead of on the menu.
I've been trying very hard to get the SearchView widget to expand in the actionbar using the support-v7 libraries. I've managed to get it working without the support libraries when I target 4.0+ but I want to write the app for 2.3+ so I need to use the support libraries.
I created a blank new activity with the following menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
<item
android:id="#+id/action_search"
android:icon="#android:drawable/ic_menu_search"
yourapp:showAsAction="always"
yourapp:actionViewClass="android.support.v7.widget.SearchView"
android:title="Search"/>
</menu>
This does not even show the search button, let alone expand it upon clicking. It simply add's the search into the menu instead of showing it in the actionbar.
Alternatively I tried the same without the appcompat library , I simply replaced the menu.xml with:
<item
android:id="#+id/action_search"
android:icon="#android:drawable/ic_menu_search"
android:showAsAction="always"
android:actionViewClass="android.widget.SearchView"
android:title="Search"/>
And it works perfectly fine, and even expands to the search text input widget upon clicking.
I want the searchview as available in the second picture while using the appcompat library, but for some reason it doesn't seem to be working. I'm using eclipse and I've included the Support libraries with resources exactly as specified in Support Library Setup[developer.android.com].
My manifest file has minsdk version as 7, targetsdk version as 18, and the build target is also 18.
I suspect something is amiss in the support library setup, can someone please tell me what I might be doing wrong? Thanks!
Maybe SearchView wasn't showed because you missed to add a collapseActionView in this line: yourapp:showAsAction="always".
Also, your activity must extends AppCompatActivity. So, add AppCompat library to project
More details you can read on this link
Hope it will help you.
I am using the actiobarcompat sample in my application and I am trying to implement search for pre 3.0 devices.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_search"
android:orderInCategory="1"
android:title="#string/menu_search"
android:icon="#drawable/ic_home"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView" />
</menu>
On Honeycomb+ this works fine, the searchview widget appears in the actionbar. What I am trying to do is have a second menu XML so I can fall back to the old search activity way of doing it. However, there is no such thing as menu-v11 folder as the menu folder is essentially menu-v11 because that is the version it started supporting this.
My question is, using the actionbar compatibility sample, is there a way to declaratively add an alternate button for pre-honeycomb?
Can you please be more specific about what you are trying to achieve?
It it's about calling different activities depending on API version, the action bar has nothing to do with that.
You analyse API version in onOptionsItemSelected and act accordingly.
If you want different menu items depending on API version, just create a folder menu-v11 (or menu-v14) and put
version-specific xml-s- there.
BTW, I use com.android.actionbarcompat and it works great for me!
I'm currently working on a mp3 library on Android. Thing is, there are 4 tab, representing Songs, Albums, Artists and Playlists. My main activity creates a tabspec for each tab and add them to a tabhost.
The problem is, I can't use the ActionBar. I tried the tutorial from Android Developpers and it works fine, however when I try to apply it on my project it doesn't work. I can't see the actionbar and if I try a getActionBar() in my activity it returns null.
As in the tutorial I put this code in the activity :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.actionbar_menu, menu);
return true;
}
And I created this menu :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_save"
android:title="Hello !"
android:showAsAction="ifRoom" />
</menu>
I suppose there are more things to do in order to display the actionbar, yet I can't find an accurate on the Web since now ... Does this have something to do with the xml from my main activity ?
Thanks for your attention
For me I added this to my manifest:
android:theme="#android:style/Theme.Holo.Light.DarkActionBar" >
Hopefully that saves some time from having to look it up.
The action bar is only available from Android 3.0 and above, and you need to set your application theme to Holo.
Besides setting the sdk version, for instance:
<uses-sdk android:minSdkVersion="11" android:targetSdkVersion="15"/>
You also have to make the application theme use Holo.
make android:minSdkVersion="14" it works for me.