Force menu inflating behaviour outside the action bar - android

I'm currently developing an android application, whose target is API19 (min sdk version is set to API14). This app will not run in a mobile phone, but on a custom device.
The screen is not a touch screen, so user interaction will rely on a keyboard, and also I have to mention that this app won't have action bar implemented (I'm currently hidding it).
I'm trying to implement the old style menu behaviour, where you could bring up the menu by pressing the menu button on your device (deprecated since API10), like this:
So, things get complicated when I try to inflate the menu, as android will automatically inflate my menu in the action bar (since API11):
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
QUESTION:
I know it's deprecated, but still I want to implement the menu that way, therefore, is there any way I can force android to inflate this menu "the old way", without lowering my target API ?

Related

onPrepareSubMenu in a custom ActionProvider never called

Before I ask the question let me give some additional info:
I have a Fragment which adds a MenuItem in the onCreateOptionsMenu. The added MenuItem also has a custom ActionProvider added to it.
The custom ActionProvider doesn't use a ActionView but instead prepares a SubMenu with certain items to choose from. For this reason I've setup the ActionProvider as follows:
I return null in the onCreateActionView method
hasSubMenu() returns true
In onPrepareSubMenu(SubMenu Menu) I first clear the current menu, afterwards add the needed items
I correctly handle the onMenuItemClick in the ActionProvider
Since I'm using ActionBarSherlock, my ActionProvider extends com.actionbarsherlock.view.ActionProvider rather then android.support.v4.view.ActionProvider (don't know if it should make any difference, but at this point I don't know what does)
This all goes well on devices with Android versions higher then 3.0. I see the added MenuItem, it has the correct SubMenu (from the bound ActionProvider) and the correct actions take place for each menu option. But with devices running Android versions below 3.0 (I could only test this on a device running 2.3.6) something weird happens; hence the following question.
Google clearly states:
"onPerformDefaultAction()
The system calls this when the menu item is selected from the action overflow and the action provider should perform a default action for the menu item.
However, if your action provider provides a submenu, through the onPrepareSubMenu() callback, then the submenu appears even when the action provider is placed in the action overflow. Thus, onPerformDefaultAction() is never called when there is a submenu."
Taken from: http://developer.android.com/guide/topics/ui/actionbar.html#CreatingActionProvider
From the excerpt I take it onPerformDefaultAction() should NEVER be called in my custom ActionProvider. Yet on devices running Android version 2.3.6 the onPerformDefaultAction() DOES get called, which also prevents the SubMenu from showing.
My question is; why does the onPerformDefaultAction() gets called instead of the onPrepareSubMenu(SubMenu Menu)? I need a submenu on devices running Android 2.3.6 as well..
EDIT:
I managed to fix my problem using the same technique from the SubMenus.java from the ActionbarSherlock demo-code. This involves adding a SubMenu instead of a custom ActionProvider to the menu in onCreateOptionsMenu(Menu menu), and attaching OnMenuItemClickListener to each MenuItem there.
The workaround is nice and simple. Still, this does not answer my question as to why custom ActionProviders do not work.

android overflow menu in bottom bar

I'm trying to get the overflow button from the action bar to show up in the bottom, next to the back/home/other-apps buttons. I was assuming this would be the case if I turn off the title bar, but it doesn't. I tried googling it, but I can't find any solutions.
Who can give me a hint?
For example this app has it:
In your AndroidManifest you have to set the targetSdkVersion to 13 or earlier like this:
<uses-sdk android:targetSdkVersion="13" />
That will force ICS to run your app in compatibility mode and give you the 3 dots in your bottom bar for your overflow menu.
Adding the overflow menu button to the system navigation bar is done to support apps targeting older version of android (2.3 and older) that don't have a hardware Menu button.
http://android-developers.blogspot.com/2012/01/say-goodbye-to-menu-button.html
The article above provides the logic when the system places the overflow button in the nav bar. It doesn't say how to force it to appear though for all devices. but it may help.
Did you try this code for creating option menu?
//Menu options within the app.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

how action bar detect where menu button must show

If application with Action Bar run on tablet - there are menu button in right corner. But on smartphone this button don't show, because device has a hardware menu button, I think.
I need impement similar behavior in my code to show my custom menu button only on tablet, and don't show it on smartphone? It is real?
I don't want use action bar
Thanks
You can check if the device is tablet or smarphone, and inflate the option menu only if the device is tablet.
Suppose you have a method isTablet() that returns true if the device is tablet.
Then you need to override the onCreateOptionMenu() and inflate the menu only if isTablet() returns true.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if(isTablet()){
menuInflater.inflate(R.menu.menu_tablet, menu);
return super.onCreateOptionsMenu(menu);
}else{
// do nothing
return true
}
}
How to determine if a device is tabled or phone, you'll need to dig this article: http://developer.android.com/guide/practices/screens_support.html

Android sdk. inflate menu(xml file) on button press

I understand that after Android 3 menu buttons are not supported. So, I simply want to use a buton on the screen to inflate the menu. I currently use an overridden
public boolean oncreateoptionsmenu(Menu menu) { MenuInflater inflater=getMenuInflater();
inflater.inflate( R.menu.menu, menu); return true}.
This script runs on menu button press, but I also want it to run on my button press. How can this be done? What data is sent to the Menu parameter ( the menu to inflate to)?
Thanks,
On android 3+ the menu will be added to the actionbar automatically when the activity starts. There is no need to change anything.

Android default menu button is not visible

in my app the android default menu button is not visible? why? Any help?
Refer this:
On Android Honeycomb how do I add option menu to the home bar at the bottom of the screen?
And this:
onCreate Options menu is not showing in android 3.1
You can simple edit target SDK in manifest file to force use SDK of mobile rather than tablet like this:
android:targetSdkVersion="10"
Not only should your apps stop relying on the hardware Menu button, but you should stop thinking about your activities using a “menu button” at all.Your activities should provide buttons for important user actions directly in the action bar (or elsewhere on screen). Those that can’t fit in the action bar end up in the action overflow.
In order to provide the most intuitive and consistent user experience in your apps, you should migrate your designs away from using the Menu button and toward using the action bar.
Ice Cream Sandwich rolls out to more devices, it’s important that you begin to migrate your designs to the action bar in order to promote a consistent Android user experience.See this link
Did you hook up a xml menu file to your activity ?
#Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
getMenuInflater().inflate(R.menu.xmlfilename, (android.view.Menu) menu);
return true;
}

Categories

Resources