Inflate ActionBarSherlock Menu's defined in XML - android

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;
}

Related

Menu item always goes in the overflow menu in Android

I have a menu icon for my ActionBar, but it always goes into the overflow menu no matter what I do. I want it to show up as an icon and not go into the overflow menu.
Here is my menu_daily_selfie.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/camera_button"
android:icon="#android:drawable/ic_menu_camera"
android:title="Camera"
app:showAsAction="always"/>
</menu>
I'm using the appcompat library. What gives? Seems like this should work.
Here is my onCreateOptionsMenu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_daily_selfie, menu);
return true;
}
My main activity extends ListActivity since it uses a ListView
Then you are not using appcompat-v7 properly. Either:
Switch to inheriting from AppCompatActivity, and manage your own ListView, or
Stop using appcompat-v7, and switch your menu resource to use android:showAsAction instead of app:showAsAction

How to update the text of menu in android actionbar

What i have::
I have a menu in actionbar it has two sub-items in it
buffet_map_cart.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_cart"
android:icon="#drawable/ic_action_camera"
android:showAsAction="ifRoom"
android:title="cart">
<menu>
<item
android:id="#+id/action_cart_buy"
android:icon="#drawable/ic_action_camera"
android:title="Sort by newest"/>
<item
android:id="#+id/action_cart_reserve"
android:icon="#drawable/button_reserve_selector"
android:title="Sort by rating"/>
</menu>
</item>
</menu>
What i am trying to do::
I want to update the text dynamically for the menu buttons
action_cart_buy and action_cart_reserve
How to find this resource from the Java code and update the text ?
You can use onPrepareOptionsMenu(Menu) for dynamic updates:
Prepare the Screen's standard options menu to be displayed. This is called right before the menu is shown, every time it is shown. You can use this method to efficiently enable/disable items or otherwise dynamically modify the contents.
You can use it like:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem buyItem = menu.findItem(R.id.action_cart_buy);
MenuItem reserveItem = menu.findItem(R.id.action_cart_reserve);
buyItem.setTitle("New buyItem String");
reserveItem.setTitle("New reserveItem String");
return super.onPrepareOptionsMenu(menu);
}
Take a look at findItem from Menu and than MenuItem, there is a setTitle.
Edit--
There is a topic called "Changing menu items at runtime" in this article which may help you as well.
You have access to the Menu object in onCreateOptionsMenu or onPrepareOptionsMenu (better).
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem item = menu.findItem(R.id.action_cart_buy);
item.setTitle("Any title");
return super.onPrepareOptionsMenu(menu);
}
To make the UI update your ActionBar at the right time, you have to call:
invalidateOptionsMenu();

Android option menu is not showing on API 2.x.x

Im using appcompat v7 for rendering the action bar on my Android project. This works very well on versions 4.x but the options menu (only the options menu) is not exibithed on 2.x.x versions. What is the problem?
My list_team.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/action_profile"
android:title="#string/menu_label_profile"
android:orderInCategory="100"
android:showAsAction="never"
app:showAsAction="never"/>
<item
android:id="#+id/action_about"
android:title="#string/menu_label_about"
android:orderInCategory="100"
android:showAsAction="always"
app:showAsAction="always"/>
My Activity
public class MyActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_team);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.list_team, menu);
return true;
}
}
Appreciate any help.
Try to delete these two attributes android:showAsAction="" and keeping only these app:showAsAction="" instead.
According to the Google Documentation:
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.
You can also change the orderInCategory of *action_about* to 1.
Based on this code, you are missing super.onCreateOptionsMenu() in your override:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.list_team, menu);
return super.onCreateOptionsMenu(menu);
}
And you should also only use app:showAsAction namespace as has been previously suggested.
Devices with Android 2.X usually have a hardware menu button. In that case the overflow menu icon is not displayed (at least by default, not sure if there is a toggle for this in appcompat, though there was in ActionBarSherlock).
Nevertheless, tthe options should be accessible when pressing the menu button. May that be the case?

How can I create a dropdown options menu from my action bar item?

I am using actionbarsherlock and have set up an item in my action bar. Now I would like that on click on that item, a dropdown menu appears that shows two more options. What should I do? This is my code so far:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.activity_main, (com.actionbarsherlock.view.Menu) menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
// ?????
// ?????
}
}
Per the Defining Menus via XML Guide:
You can add a submenu to an item in any menu (except a submenu) by adding a <menu> element as the child of an <item>. Submenus are useful when your application has a lot of functions that can be organized into topics, like items in a PC application's menu bar (File, Edit, View, etc.).
They give an example XML of:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/file"
android:title="#string/file" >
<!-- "file" submenu -->
<menu>
<item android:id="#+id/create_new"
android:title="#string/create_new" />
<item android:id="#+id/open"
android:title="#string/open" />
</menu>
</item>
</menu>
In this case, your onOptionsItemSelected would look for create_new and open actions (and the file item would be handled by the menu itself).

ActionBarSherlock menu item showing up on tablet not on phone

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.

Categories

Resources