So I created a new project and the following came up as part of the default start code.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.phone_start_screen, menu);
return true;
}
Coincidently I am trying to learn Android coding and there are parts similar to what I'm currently reading, fragments. However I do no understand the code above, I do not even know when it is being called and well I do not understand fragments but thats another matter and I will continue trying to learn that. At the moment I would just like an explanation on the code above.
onCreateOptionsMenu usually gets called when the user presses the Menu button. In the case of an action bar presence though, the method will actually get called before onCreate finishes.
As to what code does, it inflates the phone_start_screen.xml (in your example) from the /res/menu folder.
Related
I am trying to hide a MenuItem of my menu but without success.
This is the code that I have right now:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu){
MenuItem item = menu.findItem(R.id.lastOption);
item.setVisible(false);
invalidateOptionsMenu();
return true;
}
but it still shows that option when I run the application.
Trying to find why this behaviour happened, I tried to set some breakpoints to my code and found that menu variable has a variable called mVisibleItems. In that variable I can see that the item that I tried to hide does not appear. It appears on the application, though.
So, I cannot understand why if it does not appear on the menu visible variables, it still is shown on the application.
Am I missing something?
Thanks in advance!
invalidateOptionsMenu();, calls onCreateOptionsMenu again. From the documentation
Declare that the options menu has changed, so should be recreated. The
onCreateOptionsMenu(Menu) method will be called the next time it needs
to be displayed.
so, after setting your item to false, you are inflating the layout of your menu, again
I suppose that this is not the better way to do this but I could not do it using onPrepareOptionsMenu.
As I need two types of menu depending of the user that had logged in (admin or user) I finally do this creating two types of menu for my application. One with some options and one without the options that I do not need for a normal user. Then, on onCreateOptionsMenu I inflate the menu that I need depending of the user that has logged in.
This is the code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
if("admin".equals(typeUser)){
getMenuInflater().inflate(R.menu.activity_main_admin, menu);
}else{
getMenuInflater().inflate(R.menu.activity_main_user, menu);
}
return true;
}
where typeUser is a String that I send from the Login Activity in which I set a bundle depending of the user that has logged in.
I also have created two activity_main layouts changing the menu option of the NavigationView on the XML:
app:menu="#menu/activity_main_admin" or app:menu="#menu/activity_main_user", depending on the layout.
Finally, I also have created a condition to set the layout on the onCreate method of MainActivity depending of the user that has logged in. This is the code to do this:
if("admin".equals(typeUser)){
setContentView(R.layout.admin);
}else{
setContentView(R.layout.user);
}
I want to know if there's any way in Android to programatically know if
public boolean onCreateOptionsMenu(Menu menu)
has been called and returned.
I'm using actionsherlockbar library in a project and the app has to download some content and I'd like to start the download when the action bar is loaded so I can use its integrated progress interface, but I can't find a way since onCreateOptionsMenu is called after onCreate.
To sum up, hat I want to do is:
-load the actionBar
-notice it is loaded
-enable the progress bar
-download
-disable the progress bar
But I don't know how to notice it's loaded.
Thanks in advance for your help.
use Log.d() method to register in the logcat. eg:
in your case:
where you have defined the onCreateOptionsMenu() add this command:
Log.d("tag of your choice", "value you want to check if it has been initialized");
You can see more about this function in eclipse or https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&ved=0CCwQFjAA&url=http%3A%2F%2Fdeveloper.android.com%2Freference%2Fandroid%2Futil%2FLog.html&ei=3RIfUqaCGcS4rgf19ICQCg&usg=AFQjCNEd4hTMzjI2BnTS2woBfjvrddoQsw&sig2=4JDVD0JjIjx0mEhog9plPQ&bvm=bv.51495398,d.bmk
Here is my question, and I'm sure its a basic one. please be patient with me since its been over 2 years since I've programmed.
I created an Action bar in my MainActivity class. In my onOptionsItemsSelected(Main menu) method, I have a switch statement that tells MainActivity which activity to start based on user input from the action items that are available. I am interested in having the Action Bar maintain consistent behavior across all activities when the user chooses a selection. The menu items stay the same, I want to have the onOptionsItemsSelected be the same across all activities. I could copy and paste the code from MainActivity, but I know that isn't the 'correct' way to do it.
For example:
In MainActivity action bar gives option A,B,C. Activity A,B, or C starts when user selects it.
If a user selects A, A starts. I want to user to be able to start B,C or main screen using the action bar.
I know that this has a basic solution, but its been a while since I've coded. All help is appreiated. Feel free to be snarky due to the simplicity of my question :)
I figured out the answer, and it comes down to basic programming. Please let me know if this comes out clear enough.
So there are a few things to know about the Android format of programming. From MainActivity(), when it starts it calls two functions in order:
OnCreateOptionsMenu(Item item) which calls the values to put in the menu / action bar.
OnOptionsItemsSelected(Item item) which tells the Application what to do with the selection.
If you want to keep the action bar consistent across each of the different views, in each activity when you call OnOptionsItemsSelected(), you have the method that gets the id from the choice, then you return it using super.OnOptionsItemsSelected(item). In order for this to work, the user should have the calling class extend MainActivity instead of Activity. Here is an example of the code:
This is the code from a 2nd activity that wants to keep the same behaving action bar (with the same choices) that MainActivity has. Note: if you don't extend MainActivity it won't work because it will call the method from Activity class, not your MainActivity.
public class secondActivity extends MainActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondActivity);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
item.getItemId();
return super.onOptionsItemSelected(item);
}
}
I just started using Juno, and I was wondering what this method is really about. And since I
dont use it, I dont want it to create every time i create a project. How can I disable her?
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
All this does is create your menu that shows up when you press the menu key. It may also show up in the action bar depending on how your app is set up and the API version it is running on. It is not needed unless you have extra options that you want to show to the user in this manner.
This method is by default created from the ADT Activity templates.
I am using tabs, with Activity groups in each. I want to add an options menu to certain activities but it won't show on any. I have options menus working in other projects with the exact same code, so I can't figure out why they won't show up. This is the code I am using:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("Option 1");
menu.add("Option 2");
menu.add("Option 3");
return super.onCreateOptionsMenu(menu);
}
Is there anything wrong with this code or anywhere else I should be looking at that might be blocking this menu from showing when I hit the menu button?
The only thing that would be blocking the menu AFAIK would be if you are overriding onKeyDown. Make sure you aren't overriding that method and thus preventing the menu button from doing what it is supposed to.