how action bar detect where menu button must show - android

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

Related

Force menu inflating behaviour outside the action bar

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 ?

Android invalidateOptionsMenu not updating Action Bar

I'm targeting Android >=4 and I have an issue with the ActionBar.
I need to dynamically update the action bar and all the code seems to work fine on a Nexus 7 and Nexus 4. The problem I have is with an Xperia S (Android 4.1.2). The Nexus devices update the ActionBar immediately. The Xperia only updates when the hardware menu button is pressed.
I call invalidateOptionsMenu() which in turn successfully calls the onPrepareOptionsMenu(Menu menu) on all devices. The difference is that the Xperia is simply not updating the display. As soon as I click the hardware menu button, up pops the menu items and buttons.
I've read quite a few posts and tried numerous methods - I simply can't get the Xperia to behave properly.
I've messed around with this and the best I can come up with is to recreate the activity which gives and unacceptable user experience.
Everything works fine on devices with soft menu buttons. Until I can come up with something better I've decided to force all menu items on to the action bar drop down menu for devices with a hardware menu button.
I detect the hardware menu button in the onCreate of the Activity;
this.hardwareMenuButton = ViewConfiguration.get(getApplicationContext()).hasPermanentMenuKey();
Then I have something like this;
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem menuItem = menu.add("Refresh");
if(this.hardwareButton) {
menuItem.setShowAsActionFlags(android.view.MenuItem.SHOW_AS_ACTION_NEVER);
} else {
menuItem.setIcon(R.drawable.ic_action_refresh);
menuItem.setShowAsActionFlags(android.view.MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
return super.onPrepareOptionsMenu(menu);
}

What does onPrepareOptionsMenu do?

I want to make Option Menu for Android, I have visit this site. In their script, I found onPrepareOptionsMenu, I try to compile and run using Android 2.3.3 compiler with and without onPrepareOptionsMenu, both works, but I didn't see any difference.
public boolean onCreateOptionsMenu(Menu menu){
//code here
}
public boolean onOptionsItemSelected(MenuItem item){
//code here
}
public boolean onPrepareOptionsMenu(Menu menu){
//code here
}
What is actually onPrepareOptionsMenu method do? Is that method important? Could I just delete the method?
Addition
Oh, I also hear about Action Bar in Android 3.0, it says that Action Bar is the alternative way for make Option Menu, and it using onPrepareOptionsMenu. Is that right?
Thank you...
Take a look in the API:
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.
If you want to alter the menu before it's shown to the user, you can put code to do that into onPrepareOptionsMenu. I've used that dynamically to disable some menu options in some circumstances.
As an example of when one might want to disable a menu option, I had an app where there was a way of specifying a destination. One of my menu options was to calculate a route to the destination. However, if a destination wasn't specified, that option didn't apply, so I used onPrepareOptionsMenu to disable that menu option when it wasn't applicable.
From Android 3.0 and beyond, there's the ActionBar, which is a menu bar. The most important items go into the ActionBar itself, but then there's an overflow for when there's not enough room on the action bar. One can specify that menu items should always be in the overflow menu and never on the action bar itself. On some devices, the action bar overflow corresponds to the permanent menu button on the device, whereas on other devices which don't have a menu button the overflow menu is seen on the right hand side of the action bar as three vertical dots.
onCreateOptionsMenu is called once, when your activity is first created. If it returns false, no option menu is shown and onPrepareOptionsMenu is never called.
If onCreateOptionsMenu returns true, onPrepareOptionsMenu is also called before the activity is displayed, and also every time the options menu is invalidated. Use onPrepareOptionsMenu if you need to enable/disable, show/hide, or add/remove items after creating it.
If your menu does not change, use onCreateOptionsMenu.
example
#Override
public void onPrepareOptionsMenu(#NonNull Menu menu) {
super.onPrepareOptionsMenu(menu);
if(!URLUtil.isValidUrl(news.geturl())){
menu.findItem(R.id.share).setVisible(false);
}
}

Menu on Fullscreen Activity

I have a fullscreen Activity launched by this way:
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.preview);
I want to display a menu on the bottom of the screen, by this way:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.menu, menu);
return true;
}
with the uiOptions (AndroidManifest) splitActionBarWhenNarrow.
But nothing appears, what is the problem? Thanks
This behavior is because the "hardware" menu button is being phased out in favor of the ActionBar, see this Android Developers blog posting for more information.
Additional information on the ActionBar can be found at:
http://developer.android.com/design/patterns/actionbar.html
You are trying to inflate a layout as a menu
inflater.inflate(R.layout.menu, menu);
Perhaps it should be
inflater.inflate(R.menu.menu, menu);
I had a similar problem when trying to up the TargetSDK on an App that previously had worked fine. I had a full screen app where the user could draw on the entire screen and save from the options menu, but in trying to up the TargetSDK to bring new features to the device I found I lost this feature and all the internet advise was to implement an ActionBar.
After much thought I decided the best option was to implement my own menu from onBackPressed, and show the user a Handy Tip to alert the user of this somewhat odd behaviour when the Activity Was first launched. Obviously the menu needs to have an Exit option or exit the activity when back is pressed again.
It goes against standard Android behaviour, but my only choices were to put an unwanted ActionBar into my full screen activity and not make it full screen any more, or leave the app as a "legacy app" and put no new features in it.
You have problem in following line
inflater.inflate(R.layout.menu, menu);
change to
inflater.inflate(R.menu.menu, menu);
**Also make sure you have not saved
menu.xml
under
layout folder
.save it in menu folder.**
Path will be res->menu->menu.xml

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.

Categories

Resources