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.
Related
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.
Should be simple enough but might not be.
When using action bar in Android 3.0+ you have the option of defining your menu items in XML or in code. I prefer to code them in xml as action bars feel more UI based than functional.
On an average day, you would use this to inflate the xml into a menu
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Menu is defined inside 'res/menu/...xml
getMenuInflater().inflate(R.menu.activity_home, menu);
return true;
}
And your XML file would look like this
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/menu_settings"/>
<item
android:id="#+id/menu_item_menu"
android:icon="#drawable/menu_off_128"
android:showAsAction="ifRoom|withText"
android:title="#string/inbox_string"/>
<item
android:id="#+id/menu_item_gallery"
android:icon="#drawable/gallery_off_128"
android:showAsAction="ifRoom|withText"
android:title="#string/gallery_string"/>
<item
android:id="#+id/menu_item_inbox"
android:icon="#drawable/inbox_off_128"
android:showAsAction="ifRoom|withText"
android:title="#string/inbox_string"/>
<item
android:id="#+id/menu_item_contact"
android:icon="#drawable/phone_off_128"
android:showAsAction="ifRoom|withText"
android:title="#string/contact_string"/>
</menu>
Right now, I'm faced with the problem of making an actionbar backwards compatible and actionbarsherlock seems to be the most pleasant to use and popular.
So I tried the above with actionbarsherlock and sadly there are compile time issues.
Namely that the Menu class returned by the inflater is from 'Android.view.menu' and not the 'com.actionbarsherlock.menu'. I went digging through the samples on github but all of them have the menu defined in code.
So has anyone manged to get an actionbarsherlock menu working with an XML file based layout?
try this
#Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
getSupportMenuInflater().inflate(R.menu.your_menu, menu);
return true;
}
Just had this problem myself.
What you want to to do is call getSupportMenuInflater() instead of getMenuInflater() like so:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
Sometimes when doing some so very simple, you miss something big. I must be missing something huge because I am getting nowhere fast (and an hour of sifting through the web has revealed nothing).
I want to have a menu with items with checkmarks in group--just like a simple RadioGroup layout. I get the menu, but no checkmarks of any kind.
Here's the res/menu/options_menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<group android:checkableBehavior="single">
<item
android:id="#+id/item1"
android:title="item1"
/>
<item
android:id="#+id/item2"
android:title="item2"
/>
<item
android:id="#+id/item3"
android:title="item3"
android:checked="true"
/>
</group>
</menu>
And of course, here's the relevant methods in my Activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
return true;
}
This is about as simple as I can make it--any ideas on what I'm missing?
I think you try to create Options Menu (it's displayed by click menu button) and according to this
Note: Menu items in the Icon Menu (from the Options Menu) cannot
display a checkbox or radio button. If you choose to make items in the
Icon Menu checkable, you must manually indicate the checked state by
swapping the icon and/or text each time the state changes.
you can't add group menu in OptionsMenu. so i think you shoud use Context Menu or Submenu.
Take a look at this article
Here's the work-around:
In your strings.xml file you can embed a unicode checkmark. There are two to choose from. For this project I prefer the friendlier check of \u2714. You then swap a string with the check-mark for a string without it via onPrepareOptionsMenu() as appropriate.
Here's the xml code two strings, one with and one without checkmarks:
<string name="opp_random">unchecked</string>
<string name="opp_random_check">\u2714 checked</string>
Happy coding!
How about
android:checkable="true"
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....
Many of menus have always one item same to all.
Is there the way to define that item as extra menu and include it to all other?
Something like this:
menu/main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_main"
android:title="#string/text_mainmenu" />
</menu>
menu/other.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
parent="#menu/main">
<item android:id="#+id/menu_other"
android:title="#string/text_othermenu" />
</menu>
I know, that it's possible to do it programmaticaly, but I thought xml-way is nicer.
Inflating each menu and calling to super works great!
Here is an example:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.main, menu);
getMenuInflater().inflate(R.menu.other, menu);
return true;
}
You can control the order if super also adds more items by calling it before/after other inflates, or not call at all it to ignore those items.
AFAIK, <include> only works for layout XML, not menu XML, though you can certainly give it a try. Lacking that, the programmatic option (inflating both menu XML files) is the only option I am aware of.