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....
Related
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.
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.
On a tablet in landscape, the GMail app has the following:
The magnifying glass menu item appears to be aligned to the right side of the list fragment.
How can I achieve this in my own app?
Step I: Create a xml in the res/menu folder.
Step II: Add the following code snippet in the menu_main.xml or the pre-provided menu.xml
<?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/action_search"
android:title="#string/action_search"
android:icon="#drawable/ic_action_search_hdpi"
yourapp:showAsAction="always"
android:enabled="true"
yourapp:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
`
Step III: Inflate the menu.xml in the onCreateOptionsMenu(Menu menu)
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.menu_main, menu);
return true;
}
Since Search is also a menu option, android already provides the widget to use it seamlessly.You can also handle a click listener to integrate the search mechanism into the app with the help of API's.
I hope it helps.
I am using ActionBarSherlock to create a menu (with the three dots) at the top and then have a few items as a submenu. The menu is showing up on tablets, but not on a four inch phone. The app can run on sdkVersion 9-17.
My menu.xml is:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_weather"
android:title="#string/weather"
android:icon="#drawable/weather_tab"/>
<item
android:id="#+id/menu_emergency"
android:title="#string/emergency_nums" />
<item
android:id="#+id/menu_suggest"
android:title="#string/friend" />
<item
android:id="#+id/menu_support"
android:title="#string/faq" />
<item
android:id="#+id/menu_about"
android:title="#string/about" />
</menu>
And I have a menu inflate of:
public boolean onCreateOptionsMenu(Menu menu)
{
getSupportMenuInflater().inflate(R.menu.menu, menu);
return true;
}
Thanks in advance for any help.
In your res/menu/menu.xml, your item can have a android:showAsAction parameter, which defines when to show them. You can then set a bunch of parameters that will help you displaying them (or not) more easily on phones and tablets. If you want to always show the menu item, just set this parameter to "always".
More here :
http://developer.android.com/guide/topics/resources/menu-resource.html
Try
public boolean onCreateOptionsMenu(Menu menu)
{
getSupportMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
I kept searching and this is what finally worked for me: https://stackoverflow.com/a/11438245/2291915
I hope this can help someone else. I hadn't realized because I was using this on an emulator it was really about whether there was hard or soft buttons.
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.