Option menu and overflow menu issue - android

I am developing one application in which I'm using Sherlock Actionbar library for 4.0 effect in lower device, in this app there are 5 items in Menu and it is appear at option menu. When I press menu in actionbar its getting me the item list correctly but when I click on Overflow menu button(hardware menu)it doesn't give me any option.I need all items in both menu as it is.
I have tried with android:showAsAction="never" and it appears in overflow menu but not in option menu that appear in actionbar. and if I do android:showAsAction="ifRoom|withText" then it only appears in option menu in action bar not in Overflow menu so can you please find the solution for this.

Have you overridden your OnCreateOptionsMenu()? You need this for menu button support.
This is mine:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.activity_main, ((MenuWrapper) menu).unwrap());
return true;
}
Edit: and onOptionsItemSelected(), too?
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_logout:
logout();
return true;
case R.id.menu_settings:
openSettings();
return true;
case android.R.id.home:
getSlidingMenu().toggle();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

Related

action bar Menu Item

in my app I have a refresh button. clicking on it it rotates (animation) while the fragment is updating. suppose I navigate from the given fragment to another and then back. Is there any possibility when I navigate back the refresh Menu Item being clicked automatically without pressing on it.
menu item onclick method
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_refresh:
item.setActionView(getAnimation());
getCurrency(code,item);
mViewpager.setVisibility(View.INVISIBLE);
Log.i("TAG", "refresh pressed =>");
return true;
}
// Handle your other action bar items...
return super.onOptionsItemSelected(item);
}
I want this code to execute when navigating back without pressing the menu Item
Yes, you can execute all the actions your are doing when the optionMenu creation method starting from the animation and the real updating as following:
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_options_menu, menu);
//Perform the updating action
item.setActionView(getAnimation());
getCurrency(code,item);
mViewpager.setVisibility(View.INVISIBLE);
Log.i("TAG", "refresh pressed =>");
super.onOptionsItemSelected(item);
return true;
}

Open menu thing on android

on android phones there is a button at the bottom left. this usually opens up a menu. what is the code for this. Its okay to lead me to another post instead of posting a long answer here. sorry Im asking. I understand if there are other posts like this one. I just couldnt seem to find one. ill delete this one when done. Thanks.
Check out the developer guide on menu :) http://developer.android.com/guide/topics/ui/menus.html
To specify the options menu for an activity, override onCreateOptionsMenu() (fragments provide their own onCreateOptionsMenu() callback). In this method, you can inflate your menu resource (defined in XML) into the Menu provided in the callback. For example:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.game_menu, menu);
return true;
}
To handle click events:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.new_game:
newGame();
return true;
case R.id.help:
showHelp();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

Adding a 'Add Button' to the actionbar at the top when you already have tabs and a menu

I'm trying to add an 'Add Item' button at the top in the action bar. (To the right of the App Icon and Title).
Right under the action bar, I have two tabs that I can swipe between. I also have a menu XML file defined for the settings menu.
I thought actionbar uses a menu XML as well. So I added a actionbar menu XML, but when I use
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_CUSTOM);
actionbar.setCustomView(R.menu.actionbar);
my program crashes. I believe I'm doing this totally incorrectly.
My actionbar XML:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/item1" android:icon="#android:drawable/ic_menu_add"></item>
</menu>
I read on some tutorials that I'm supposed to add items to the actionbar and populate it via the OnCreateOptionsMenu function in mainActivity. But that's where my options menu is populated, not my actionbar.
An activity populates the ActionBar in its onCreateOptionsMenu() method.
Instead of using setcustomview(), just override onCreateOptionsMenu like this
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mainmenu, menu);
return true;
}
If an actions in the ActionBar is selected, the onOptionsItemSelected() method is called. It receives the selected action as parameter. Based on this information you code can decide what to do for example:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menuitem1:
Toast.makeText(this,"Menu Item 1 selected",Toast.LENGTH_SHORT).show();
break;
case R.id.menuitem2:
Toast.makeText(this,"Menu item 2 selected",Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return true;
}

Android- Tab mode actionbar with overflow menu

I have an app with no home/title bar, and an actionbar in tab navigation mode. I've added a menu to my app, which I want to show in Android 4+ as an overflow menu in the tab actionbar.
It sounds well and simple, but when I add the menu
// Initiating menu XML file
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menu, menu);
return true;
}
// Event handling for individual menu item selected
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_search:
// Load search fragment
return true;
case R.id.menu_add:
// Load add fragment
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I get this in landscape view, which is good:
But I get this in portrait mode, which is bad:
How do I prevent the tab actionbar from splitting in portrait mode?
EDIT: So it appears that when a menu is created in android 4, it immediately becomes an action overflow menu in the activity's titlebar. The problem is, I don't have a titlebar, just a tab navigation actionbar. So the activity creates a new title/action bar with an action overflow option.
I need to find a way to make the activity add the action overflow tab to the tab navigation actionbar. Is there a way to do it?
Thanks

Android access menu from onOptionsItemSelected

I have the following in my activity (sorry new to Java/Android):
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.options, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.selectItem:
// menu.add(...) --> how to get the menu instance?
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I am wondering, how can I access a menu object in onOptionsItemSelected? For example, how would I go about adding a new view to the options menu based on the selection of an existing menu item? Is the answer related to "onPrepareOptionsMenu"?
you should use SubMenu for such things ... remeber that you cant add submenu to another submenu ... so only Menu->Submenu is possible you can't do stuff like this Menu->Submenu->Submenu (while Submenu is Dialog with choices)

Categories

Resources