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

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.

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 action bar - disable options menu

How can I disable options menu item from my action bar?
I made my own custom bar, for display *.png logo, but i don't want to display three dots button of options menu.
I've tried to find some solution, but nothing works.
Code Monkey's answer will do what you want, but as a side effect it will also not allow you to add ANY action items to the Action Bar at all. I believe the correct answer is
#Override
public boolean onPrepareOptionsMenu (Menu menu) {
return false;
}
This is what you want.
Simply remove the public boolean onCreateOptionsMenu(Menu menu) method from your Activity.
In the java class containing the code for the app you are developing, all you need to do is remove the following method:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.main, menu);
}
This is automatically generated in your project by default. That's why you need to remove it.
Hope this helps :)
If you want this, not necessary Override onCreateOptionsMenu method in your activity.
Leave it with empty block.

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

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

Show Custom Menu On Menu Button Click

I have created an Activity that will behave like a popup menu is actually just a list of Menu Items. I then set the theme of the activity on my manifest to "Dialog" to get the popup effect. Now I need this activity to show when the Menu button is pressed on the device. I tried using onCreateOptionsMenu, then passing my Activity XML to the Inflater like so:
public boolean onCreateOptionsMenu(Menu menu){
new MenuInflater(this).inflate(R.layout.popup_menu, menu);
return (super.onCreateOptionsMenu(menu));
}
"popup_menu" is the XML for my Activity. But this didnt work. Any other suggestions??
you should start your activity there. I can tell you more if I see the code for your activity

Categories

Resources