Android appcompat actionbar menu item showAsAction not working - android

I have a menu item that is showing up on android 4.x but not on 2.x. Here is my menu.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/menu_filter"
android:title="Filter"
app:showAsAction="always"/>
</menu>
This is my actionbar style
<style name="style1_actionBar" parent="#style/Widget.AppCompat.Light.ActionBar">
<item name="android:background">#color/blue_dark</item>
<item name="android:textColor">#color/white</item>
<item name="actionMenuTextAppearance">#color/white</item>
<item name="background">#color/blue_dark</item>
</style>
Any ideas?
Edit: removed double quote typo
Could it be the fact that I am showing only text, no icons? I'm kind of stuck here.

Whew, thanks for your help guys but I managed to figure it out. It wasn't a problem with the xml, it was a problem with the onCreateOptionsMenu function.
I was using this
new MenuInflater(getApplication()).inflate(R.menu.activity_wentry_editor, menu);
instead of this
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_wentry_editor, menu);
Not entirely sure why this works but it does.

<menu xmlns:android="http://schemas.android.com/apk/res/android"
**xmlns:yourapp="http://schemas.android.com/apk/res-auto"** >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
**yourapp**:showAsAction="ifRoom" />
</menu>
Please refer to the documentation. http://developer.android.com/guide/topics/ui/actionbar.html
Using XML attributes from the support library
Notice that the showAsAction attribute above uses a custom namespace defined in the tag. This is necessary when using any XML attributes defined by the support library, because these attributes do not exist in the Android framework on older devices. So you must use your own namespace as a prefix for all attributes defined by the support library.

In my case I had to add a few lines to onCreateOptionsMenu.
Android Studio didn't let me use android:showAsAction="ifRoom" while using appCompat.
app:showAsAction="ifRoom" wasn't working and I removed it without problems.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
for (int i = 0; i < menu.size(); i++) {
menu.getItem(i).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
return super.onCreateOptionsMenu(menu);
}

If you want your app to support action bar below 3.0 you need to use app compact v7 from the support library.
Also check the link

Using the menu in an activity that extends the AppCompact it is necessary import the app context in the XML and use it:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- "Mark Favorite", should appear as action button if possible -->
<item
android:id="#+id/action_favorite"
android:icon="#drawable/ic_favorite_black_48dp"
android:title="#string/action_favorite"
app:showAsAction="ifRoom"/>
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
app:showAsAction="never"/>
</menu>
What you need to do basically is add xmlns:app="http://schemas.android.com/apk/res-auto"to the menu element in yout XML and use the showAsAction in the following format: app:showAsAction="ifRoom".
This will show the icon in the action bar, if possible.

Related

Unable to add action buttons to my action bar in android studio

I am currently closely following the steps on the website
https://developer.android.com/training/basics/actionbar/adding-buttons.html and at this stage of the tutorial it wants me to copy and paste the code to add an action search and action settings. However, when I run my application, the action search doesn't want to appear.
I have also made sure to include a .png for the icon of the action search, but still won't show.
I have also tried changing the minimumsdk version in my build.gradle from 8 to 11 as suggested by the website, but didn't work either. However, if I am not mistaken, the action bar is present in the app though since the overflow is there.
From my wild guess, it might be that the code is outdated since I have noticed a lot of things have changed since this tutorial was written. But I am still clueless about this weird problem.
you have to create an entry inside res/menu,override onCreateOptionsMenu and inflate it
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.yourentry, menu);
return true;
}
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_cart"
android:icon="#drawable/cart"
android:orderInCategory="100"
android:showAsAction="always"/>
</menu>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_settings"
android:orderInCategory="1"
app:showAsAction="always"
android:icon="#drawable/ic_action_settings"
android:title="#string/action_settings"/>
<item
android:id="#+id/volume"
android:orderInCategory="2"
android:title="Volume"
android:icon="#drawable/ic_action_volume_on"
app:showAsAction="always"/>
You need to xmlns referencing res-auto and then use it as I have used in my code. Hope this helps.

AppCompat v21, MenuInflater and showAsAction menu preference

I'm migrating my app to the newest v7 Support Library with material design support.
I've a simple menu that I want to show in my ActionBar.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/platformselect_button_settings"
android:icon="#drawable/ic_action_settings"
android:orderInCategory="1"
android:showAsAction="always"
android:title="#string/menu_settings"/>
</menu>
The problem is that the showAsAction preference is ignored, what I have to do to make it work is force the preference in onCreateOptionsMenu method:
#Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.platformselect_overflow_menu, menu);
for(int i = 0;i<menu.size();i++){
MenuItemCompat.setShowAsAction(menu.getItem(i), MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
}
return super.onCreateOptionsMenu(menu);
}
Am I doing something wrong? Why is the preference stored in XML ignored?
You need to use a different namespace when using AppCompat.
Try this:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/platformselect_button_settings"
android:icon="#drawable/ic_action_settings"
android:orderInCategory="1"
app:showAsAction="always"
android:title="#string/menu_settings"/>
</menu>
From the docs here:
http://developer.android.com/guide/topics/ui/actionbar.html#ActionItems
Using XML attributes from the support library
Notice that the
showAsAction attribute above uses a custom namespace defined in the
tag. This is necessary when using any XML attributes defined by
the support library, because these attributes do not exist in the
Android framework on older devices. So you must use your own namespace
as a prefix for all attributes defined by the support library.

Item with app:showAsAction not showing

I can't understand why wrong and incompatible (AndroidStudio tells me "Should use app:showAsAction with the appcompat library) code
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_search"
android:title="#string/action_search"
android:icon="#drawable/search"
android:showAsAction="always" />
</menu>
works perfect, but proper and compatible version like
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_search"
android:title="#string/action_search"
android:icon="#drawable/search"
app:showAsAction="always" />
</menu>
not showing my icon at all.
I'm testing on Samsung GT P5210 (android v. 4.4.2) and Nexus 7 (4.4.4)
Things you should always check when you want to use action bar are
1) Extend ActionBarActivity instead of Activity
public class MainMenu extends ActionBarActivity{
2) Have the right style selected as defined at manifest
Manifest
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
Style
<style name="AppTheme"
parent="Theme.AppCompat.Light.DarkActionBar">
</style>
3) Select the right title for showAsAction
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:**yourapp**="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
**yourapp**:showAsAction="ifRoom" />
...
</menu>
This is what most people get wrong
4) Define your Menu in Activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
If you do all the following your action bar should work.
Then you should add the onClickListener for every position...
I just reread your question and saw your problem is the complete opposite (but some pieces of my old answer still apply to your problem), so here is the updated answer:
Update:
You have imported the appcompat library in your gradle file, but you seem to support only devices newer than API Level 11 or 14? If this is the case, 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. But as your android:showAsAction attribute is working, you are using the native Activity and the native attribute call is correct, even if the lint check says it is wrong. So if you want to remove the lint error, you have to delete the appcompat lib from your gradle file and maybe change your activity theme to the native Holo Light theme, as your current theme might rely on the appcompat theme.
The answer why it isn't working with the app namespace is in the XML attribute loading for native respectively library code, which is handled in the old answer.
Old Answer
If you are using the ActionBarActivity from the support library to reach devices lower than API level 11, the main issue here is, that the ActionBarActivity recreates some of the native Android XML attributes such as android:showAsActionin its own scope, which you define with:
xmlns:app="http://schemas.android.com/apk/res-auto"
and then access them with the same attribute (here showAsAction) in the app: namespace.
So the ActionBarActivity can't see or reach the native android:showAsAction attribute as it is only looking for it in the app namespace and not the android namespace.
If you want to use the native attribute, you have to use the native Activity with a Holo Theme, which is only supported from API Level 11 and higher.
addthis:
yourapp:showAsAction="ifRoom"
or
android:showAsAction
for example:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
yourapp:showAsAction="ifRoom" />
</menu>
and in Activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_compose:
composeMessage();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
And read more here

Android Action Bar won't show items

I cannot get icons to show in the action bar.
I am using the most recent SDK with eclipse.
I am trying to do this tutorial
http://developer.android.com/training/basics/actionbar/adding-buttons.html
My MainActivity.java is
package com.example.myfirstapp;
import android.os.Bundle;
import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
}
My main.xml under menu is
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="always" />
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="always" >
</item>
</menu>
But I dont see the menu items. It looks like this
Any help on how to get these menu items shows would be useful.
Thank you.
Quoting from the docs:
The showAsAction attribute above uses a custom namespace
defined in the tag. This is necessary when using any XML
attributes defined by the support library, because these attributes do
not exist in the Android framework on older devices. So you must use
your own namespace as a prefix for all attributes defined by the
support library.
So do the following modifications:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
yourapp:showAsAction="always" />
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
yourapp:showAsAction="always" >
</item>
</menu>
I am pretty sure you must have already done this, but did you download the icons and put them inside your res > drawable folders? If you are using a dark action bar, be sure to install the light colored icons and vice versa.
I followed the same tutorial and it works for me. Your code looks correct.
Just put
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
and change
app:showAsAction="always"
to
android:showAsAction="always"
i think your problem is here:
return super.onCreateOptionsMenu(menu);
change it to this:
return Boolean.TRUE;
Source: Menus in android and an old project of mine.
I'm following the tutorial too, I think the mistake is here:
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
It should be
getMenuInflater().inflate(R.menu.main_activity_actions, menu);
It also took me a while for this issue after I followed the tutorial step by step. Funny how, my problem is that I used light color search button image while chose the dark Holo theme. Therefore, my search button simply becomes invisible (it is there) in the dark background. You may try to adjust your theme if you encouter same issue.

Android 4.3 menu item showAsAction="always" ignored

I'm using the new v7 appcompat library available starting from Android 4.3 (API level 18).
Regardless of what is specified in showAsAction for a menu item, it's not shown - it always creates the overflow menu icon, and puts even a single menu item under the menu.
Trying to add menu to an activity like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_sizes, menu);
return true;
}
And here's my menu xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_add_size"
android:title="#string/menu_add_item"
android:orderInCategory="10"
android:showAsAction="always"
android:icon="#android:drawable/ic_menu_add" />
</menu>
Is it a bug of the new support library v7, or just something wrong with the code?
I've been using the similar code with ActionBarSherlock many times before.
Probably you are missing required namespace:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:[yourapp]="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/menu_add_size"
android:title="#string/menu_add_item"
android:orderInCategory="10"
[yourapp]:showAsAction="always"
android:icon="#android:drawable/ic_menu_add" />
</menu>
Replace [yourapp] with your app name or any namespace your heart desires everywhere.
Other things worth checking:
See if your activity class extends ActionBarActivity
Check if the issue persists.
Android reference documentation: Adding Action Buttons. Here is the relevant text:
If your app is using the Support Library for compatibility on versions as low as Android 2.1, the showAsAction attribute is not available from the android: namespace. Instead this attribute is provided by the Support Library and you must define your own XML namespace and use that namespace as the attribute prefix. (A custom XML namespace should be based on your app name, but it can be any name you want and is only accessible within the scope of the file in which you declare it.)
Figured out myself. With the support library v7 the showAsAction should go under a custom namespace like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:balloonberry="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/menu_add_size"
android:title="#string/menu_add_item"
android:orderInCategory="10"
balloonberry:showAsAction="always"
android:icon="#android:drawable/ic_menu_add" />
</menu>
Also make sure that you use correct inflater in ActionBarActivity.onCreateOptionsMenu() method.
Correct solution:
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu_example, menu);
Incorrect solution:
MenuInflater menuInflater = new MenuInflater(this);
menuInflater.inflate(R.menu.menu_example, menu);
For Fragments
Menus with custom namespace will prevent showAsAction from showing.
Using "android:" prefix for showAsAction will work, even though Android Studio will remark you should use a custom name space.
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/action_add_checkin"
android:title="Add Checkin"
android:orderInCategory="10"
android:showAsAction="always"
android:icon="#android:drawable/ic_menu_add"/>
</menu>
This is using Android SDK 22 and Support v4 fragments, in case that makes any difference.
Got the same problem, but on Android 5. I have 3 items but OS ignored my attribute "always" and showed only 2 items. Here my solution:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Log.d(TAG, "onCreateOptionsMenu()");
inflater.inflate(R.menu.your_menu, menu);
for (int j = 0; j < menu.size(); j++) {
MenuItem item = menu.getItem(j);
Log.d(TAG, "set flag for " + item.getTitle());
item.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
}
}
Also make sure that you have the correct path for the namespace. It will not give you an error message if it's wrong.
I had
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/res-auto">
instead of
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto">
All I knew was that it wasn't working. Not sure how I managed to forget the /apk part of the path, but it happened. No error message, just an elusive bug to track down.
In my case, I had to remove from my app's build.gradle compile 'com.android.support:appcompat-v7:21.0.3'.
Notice: My min sdk = 14, and created project by android studio inserted my unnesessary dependancy.
After this replace you can write
android:showAsAction="always"
This might not be your case but I was using
new MenuInflater(this).inflate(R.menu.my_menu, menu);
changing it to
getMenuInflater().inflate(R.menu.my_menu, menu);
fixed the problem!
<?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/back"
android:icon="#drawable/back"
app:showAsAction="always"
android:title="#string/back"/>
<item
android:id="#id/save"
android:icon="#drawable/le_top_btn_icon_add"
app:showAsAction="ifRoom"
android:title="#string/save"/>
</menu>
don't work,
with supportLibraryVersion = '25.1.0'
compileSdkVersion = 25
see the "Warning"
Should use app:showAsAction with the appcompat library with
xmlns:app="http://schemas.android.com/apk/res-auto" less... (Ctrl+F1)
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.
I think the warn can be ignore.
add custom namespace like this to showAsAction and actionViewClass:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/search"
android:title="#string/search"
android:icon="#drawable/ic_search"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.widget.SearchView" />
I have solved it by replacing
android:showAsAction="ifRoom"
with
app:showAsAction="ifRoom"
That is menuitme xml look like,
<item android:id="#+id/action_refresh"
android:title="Refresh"
android:icon="#drawable/refresh2"
app:showAsAction="ifRoom" />
The simplest way is
Modify your code by adding
xmlns:app="http://schemas.android.com/apk/res-auto"
and change this code
android:showAsAction="always"
to
app:showAsAction="always"
and finally
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/menu_add_size"
android:title="#string/menu_add_item"
android:orderInCategory="10"
app:showAsAction="always"
android:icon="#android:drawable/ic_menu_add" />
</menu>
In Addition to #petrnohejl 's answer, in Kotlin you can just use this one line without initializing it first.
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.options_menu, menu)
return true
}
In my case, the menu item just ignored my app:showAsAction="always" or "ifRoom" and it really annoys me for hours. Then, after I searched in StackOverflow, I realized that I'm not using the correct MenuInflater.

Categories

Resources