Actionbar icon not showing despite showAsAction="always" - android

I'm trying to implement a menu in a ListActivity. Here's how I'm declaring the menu item:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/camera"
android:title="#string/camera_name"
android:icon="#drawable/cam"
app:showAsAction="always" />
</menu>
And this is what the preview shows in Android Studio:
The preview suggests everything is fine. It shows the camera icon that I put in the res/drawable folder.
Yet, when I try to run it in an emulator (Nexus 4 API 22), this is how the app shows up:
So the actual emulator is pushing the icon into the overflow menu despite being set as showAsAction="always".
This is how I am inflating the menu in the ListActivity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
Does anyone know what I'm doing wrong? Thanks in advance.

It is possibly because you have used app:showAsAction. So if you are using support library, simply add android:showAsAction beside app:showAsAction. This have to solve your problem.

Related

Menu icon on ActionBar of ListActivity

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.

Android onCreateOptionsMenu is not called

I am debugging on a Nexus, Android version 5.0
My Min SDK is 11, target SDK is 21.
I have the following XML
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/settings"
android:title="#string/settings_label"
app:showAsAction="ifRoom"/>
</menu>
And in my launcher activity I have this Java code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.i("Inside onCreateOptionsMenu", "True");
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_start, menu);
return true;
}
However that Log line never makes it into LogCat, and my menu is never displayed.
My desired effect is to have an action bar with the 3 vertical dots which when clicked by the user will show my menu item.
You can go through the tutorial from several different providers as follows which are recommended by SO
Android Developer
Vogella
AndroidHive
All of them have great examples and source code to help you out

Appcompat three dot menu not displayed on Android < 3

I would like to display three dot menu in my app also on devices with android lower than version 3.0.
But on devices with Android version < 3 is menu displayed only after menu button pressing.
I tried to find any wotking solution for this but none from them working from me.
What is most easy way how to solve it on all devices?
Im using appCompat_v7_3
Many thanks for any advice.
Here is code of menu and activity.
menu main_activity.xml
<?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/last_position_menu_btn"
android:icon="#drawable/ic_drawer"
android:title="#string/last_positions"
android:showAsAction="always"/>
<item android:id="#+id/settings_menu_btn"
android:icon="#drawable/ic_drawer"
android:title="#string/app_settings"
android:showAsAction="always"/>
</menu>
onCreateOptionMenu from main Activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity, menu);
return super.onCreateOptionsMenu(menu);
}
Your 3.0 device has a hardware menu button. Android does not display the overflow icon when there is a hardware menu button present.
See this question or the documentation.

why I do not see my Icon in Android Option Menu?

I have the following code to generate a Menu Item:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu1"
android:icon="#drawable/ic_action1"
android:title="#string/foo" />
</menu>
I see the Item in the menu, but I can not see the icon. Whats wrong?
I dont want to use it as the following:
android:showAsAction="ifRoom|withText"
then I see the icon, but I dont want it to be in the Action Bar
When you are using the ActionBar / Holo theme, you won't see any menu item icons in the overflow menus, sorry.
I think you will get the old menu back when you apply a pre-Holo theme to your activity!
try something like this
onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.your_menu, menu);
menu.getItem(R.id.your_item_id).setIcon(R.drawable.your_drawable_id);
}

ActionBarSherlock + split action bar menu + items with "withText" doesn't show text. How to fix?

I'm using ActionBarSherlock and I'm trying to add an options menu to the ActionBar.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_prev"
android:title="#string/menu_previous"
android:icon="#drawable/left_navigation"
android:showAsAction="always|withText" />
<item android:id="#+id/menu_next"
android:title="#string/menu_next"
android:icon="#drawable/right_navigation"
android:showAsAction="always|withText" />
</menu>
The icons are 32x32 pixels in the drawable-mdpi directory. When the ActionBar is split (portrait orientation), the icons show up but no text shows (plenty of room though). Long-pressing the icon Toasts the title. When the ActionBar is not split (landscape orientation), both the icons AND text show up as expected.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
Menu, MenuInflater, and getSupportMenuInflater() are all using the ActionBarSherlock implementations.
The 'Application' in the manifest declares:
android:uiOptions="splitActionBarWhenNarrow"
What can I do to make it show the title of each item when in portrait orientation?
Edit: Possibly relevant is that I'm running Android 4.0.4.
If you are using Android >= 3.0 ABS uses the system implementation of the actionbar, thus the behavior you see is correct. See #CommonsWare comment to the question as to why the behavior is that way.

Categories

Resources