How to show menu button on Android 3.0? - android

Currently, I use the phone's menu button to trigger a custom event. On Android 3.0 on the XOOM tablet the menu button does not show. How can I make the button available?

I believe you just don't target Honeycomb. I.e., android:targetSdkVersion="X" where X is less than 11 (and android:minSdkVersion too!)
This will cause your application to be considered a phone application, and the soft menu button will appear.
You should be asking yourself why you want this functionality on a tablet, though.

You can also detect whether the MENU button is present with this:
ViewConfiguration.get(context).hasPermanentMenuKey()
And if not, you can show a custom menu button in your UI.

In your res folder make sure you have a "menu" folder.. inside that folder you will need menu.xml file..... the xml file should contain information that looks similar to this:
<menu xmlns:android="https://schemas.android.com/apk/res/android" xmlns:android1="http://schemas.android.com/apk/res/android">
You can change out the item section to reflect your own menu.
Make sure you have the xml file saved...
Then go into your main code and do the following:
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
}
that's how I do it.

Related

Android: Hide 3 dots from Navigation view header at right

How to hide 3 dots from Navigation header which comes in the right of header? This could be repeated question. I found few similar questions and their answers but they were for older version of Android. I am using Android sdk 21.
Any idea how to hide that 3 dot button?
Just Remove Override Method like this
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search_and_add, menu);
return true;
}
This Override Method is responsible to for creating three dote as you mention it's really OptionMenu. If you don't want it, don't override onCreateOptionsMenumethod.
Alternative
Don't Inflate the menu xml. Just block the line like this
//getMenuInflater().inflate(R.menu.search_and_add, menu);
other code can remain same. No problem at all..
Those "3 Dots" are the "Overflow" menu, and is created when you establish a menu using a file in the menu resources directory.
If you have buttons or functionality you are wanting to expose via you action bar, you will need to have the overflow buttons (or instead, you can choose to have your buttons exposed at the top level inside the Action bar.
If you really don't want a menu, get rid of the menu.xml file describing this menu, and then get rid of the onCreateOptionsMenu() from your Activity.
Here are the official docs, which describe how this works.
I think you are speaking about the options menu, to get rid of it remove the override of the method onCreateOptionsMenu
In your menu folder the xmlfile that is used by your activity, change the app:showAsAction="never" to app:showAsAction="always" or some other you can see the options that are availabe by pressing ctrl+space.
Or else to get rid of it completely just remove the whole code and it's corresponding usages.

Do I need a menu resource for each layout resource

I have an app with an action bar and menu resource folder with an menu_main.xml file in it, I suppose I'm right in assuming that each resource file should have an activity?
If not is there a way to dynamically change the android:visible="false" to android:visible="true"? Or am I going about it the wrong way?
Menus are for Activities, not layouts.
To answer your question, there is a way to dynamically change the visibility. In your onCreateOptionsMenu (Menu menu) method, you can set individual menu components to be visible/invisible.
Eg.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
menu.findItem(R.id.myMenuItem1).setVisible(false);
}
However, I would stick with making a separate menu xml per Activity - it's small (it is just text after all), and it helps make sure that you're not mixing stuff up.

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.

How I hide the menu button for one activity

There is an activity which has no menu item. So I want to hide the menu button. Is there any way to achieve that?
Thanks
I figured out how this can be done. In your AndroidManifest.xml, you need to specify something like the following:
<uses-sdk android:minSdkVersion="8"
android:targetSdkVersion="14" />
Basically, minSdkVersion is the minimum Android SDK version that you application supports (in my case, 2.2) and targetSdkVersion is the version you're "targetting" (i.e. your "preferred" version - in my case, that's 4.0)
By default, targetSdkVersion is the same as minSdkVersion, and if you leave that pre-Honeycomb, you're basically telling Android that your app is "legacy" and it'll always show the menu button.
If you make targetSdkVersion post-Honeycomb, then the menu button will only be shown if you actually have menu items defined.
You can do using the onPrepareOptionsMenu, where you set the menu item that you want to show to user,[http://developer.android.com/reference/android/app/Activity.html#onPrepareOptionsMenu%28android.view.Menu%29][1]
[1]: http://developer.android.com/reference/android/app/Activity.html#onPrepareOptionsMenu%28android.view.Menu%29. Do not inflate using the xml just do it manually.
Thank you.
Try this to include in your code so that the menu items are not automatically visible when you start the activity.
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
//add the menu items here
return true;
}
By writing like this the menu items are visible only when you click on the menu button of the android smartphone
So I want to hide the menu button
On the vast majority of Android devices, the "menu button" is a physical thing, and cannot be hidden, any more than I can hide a mountain or a molehill.
On Honeycomb, if you do not define an options menu, you should not get the "overflow" button in the upper right corner, in the action bar.
If you are trying this on some non-compliant Android device that does its own soft menu button, you are out of luck.

Categories

Resources