Menu icon on ActionBar of ListActivity - android

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.

Related

Actionbar icon not showing despite showAsAction="always"

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.

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.

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 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.

android.support.v7 with `ActionBarActivity` no menu shows

In the new update Google has released a new API support library, that supports the ActionBar in API level 7+.
I used ActionBarSherlock until this update and I wrote the code to load the menu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main, menu);
return true;
}
and the menu file:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/item_menu_ok" android:icon="#drawable/ic_action_ok"
android:title="#string/ok" android:showAsAction="always"></item>
<item android:id="#+id/item_menu_cancel" android:icon="#drawable/ic_action_cancel"
android:title="#string/cancel" android:showAsAction="always"></item>
</menu>
To set up the menu buttons on the action bar. This code worked perfectly with ActionBarSherlock. But when I changed the action bar to the new support library, the buttons are not shown in the action bar. Even if they are set as android:showAsAction="always". And when I debug the code, the function menu.getSize() return 2, and that is correct, but no buttons are shown..
Why are the buttons not shown in the new support library?
Try pressing the MENU button on your device or emulator, and see if they appear in the overflow.
If they do, then the problem is that your <menu> XML needs to change. Menu XML that works with ActionBarSherlock and the native API Level 11+ action bar will not work with the AppCompat action bar backport.
Your menu XML would need to look like this:
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto"
>
<item android:id="#+id/item_menu_ok" android:icon="#drawable/ic_action_ok"
android:title="#string/ok" yourapp:showAsAction="always"></item>
<item android:id="#+id/item_menu_cancel" android:icon="#drawable/ic_action_cancel"
android:title="#string/cancel" yourapp:showAsAction="always"></item>
</menu>
And you would need to use the same yourapp prefix for anything else related to the action bar (e.g., yourapp:actionLayout).
You can see this covered in the action bar documentation.
I'd like to add a little to the answer.
If you want to see both text and an icon, please use withText in showAsAction
I've just tested it; when I used always or ifRoom without withText, I only saw an icon.

Categories

Resources