I created app from "Empty Activity" template in Android Studio 1.4 and added options menu.
Resource:
<?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_foo"
android:icon="#android:drawable/ic_media_previous"
android:orderInCategory="100"
android:title="#string/word_foo"
app:showAsAction="ifRoom"
/>
<item
android:id="#+id/action_bar"
android:icon="#android:drawable/ic_media_next"
android:orderInCategory="101"
android:title="#string/word_bar"
app:showAsAction="ifRoom"
/>
<item
android:id="#+id/action_baz"
android:icon="#android:drawable/ic_media_pause"
android:orderInCategory="102"
android:title="#string/word_baz"
app:showAsAction="never"/>
</menu>
Method:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_menu, menu);
return true;
}
After change the orientation app crashes. What is wrong?
Caused by: java.lang.ClassCastException: android.view.AbsSavedState$1 cannot be cast to android.support.v7.widget.Toolbar$SavedState
I readed the answer here class cast exception on orientation change (Android)
and I think it is not my case: I have absolutely empty layout. There is no widgets with id at all.
I found the problem.
I'm not native English speaker, and I forgot, that bar from sequence foo, bar, baz also has common value. And after menu item with id action_foo I created action_bar. It conflicts with some id in SDK layouts, as I presume, corresponded to ActionBar.
So solution is change id in android:id="#+id/action_bar" to something else.
Related
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.
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.
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.
I try to add an optionsMenu to my activity. I use the code from the android.developer guide :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/new_game"
android:icon="#drawable/ic_new_game"
android:title="#string/new_game" />
<item android:id="#+id/help"
android:icon="#drawable/ic_help"
android:title="#string/help" />
</menu>
and
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.game_menu, menu);
return true;
}
I use the same code in two different activities, but only in one of them the optionsmenu is displayed... posting the whole activity code wouldnt make sense, so please comment if special code segments are helpful. I hope anyone can help me! THX
EDIT: I already found the problem : I overrode onKeyDown(..) and returned true at the end, so other key-related events have not been called...
I found this issue once and I got rid off by not using the string resource but placing menu name directly in title....Check if it works for you too or not....
I'm trying to make a customized options menu. After using this code I get: Element item is not allowed here
Code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<item android:id="#+id/morsoid_settings"
android:icon="#drawable/ic_new_game"
android:title="#string/new_game" />
<item android:id="#+id/morsoid_close"
android:icon="#drawable/ic_help"
android:title="#string/help" />
</menu>
Inspired by: Android dev guide
I dont know whether it makes a difference, but did you place you menu in res/menu and not in res/layout?
Try leaving out the layout attributes. Here is the example from the documentation:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/new_game"
android:icon="#drawable/ic_new_game"
android:title="#string/new_game" />
<item android:id="#+id/help"
android:icon="#drawable/ic_help"
android:title="#string/help" />
</menu>
Edit - also make sure you are using a MenuInflater as the guide suggests:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.game_menu, menu);
return true;
}
Using a LayoutInflater will cause <menu> to be interpreted as a view element, when it is actually a menu resource.
Not 100% sure you are talking about a compile error, or the errors shown in your development system while in the layout file.
Using Idea IntelliJ (10.5) I got that error while pasting the example code shown above into the menu.xml file.
However after building the project, it disappeared. I still see the layout_width / height errors you see when editing the menu.xml file but it does not affect the build or runtime behaviour.
This is kind of an old question but I thought I'd put my solution here since I experienced this in 2019:
I was getting a warning in Android Studio saying "Element item is not allowed here" when putting an <item> inside a <menu>. It turns out my problem was that in my src/main/res/layout folder, rather than in my src/main/res/menu folder.