Adding menu resource file to different activities - android

I want to add action bar items to my activities. But I'm only being able to have the menu resource file for the Main activity.
Can someone please help me set action bar items to my activities?

In app/res/menu folder create new Menu resource file (xml file). In that file define your menu as you want it to be. And then in activity .java file include your menu in OnCreateOptionsMenu method.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.your_menu_file, menu);
return true;
}

Related

Menu in Blank Activity

How can I add Menu option in Blank Activity? I've watched lots of videos to add menu option, they keep on showing menu folder in res, while I don't see menu folder in my project. How can I add menu option?
After creating menu layout, You need to inflate your menu in the blank activity as well something like this
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_menu, menu);
return true;
}
replace my_menu xml file with your ones

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.

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.

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