i have this problem which i never expect it to occur. When i pressed on the Menu button, the program display the Menu and after i click on one of the items from the Menu, it pops out a item list for me to choose. However, just when the item list pops out, some of the code in onOptionsItemSelected() was called when i did not even select any item. After i selected on of the items, onOptionsItemSelected() got called again (i know this is called because an item was selected). Can anyone tell me how to solve this problem or even why is this happening? Below is the coding...
Btw, the 'count' is just an integer variable to show whether onOptionsItemSelected() was called as it would increment each time when called.
public boolean onCreateOptionsMenu(Menu menu) {
SubMenu sendMenu = menu.addSubMenu("Change Profile");
int size = profileNames.size();
for(int i=0; i<size; i++){
sendMenu.add(0,i,0,profileNames.get(i).toString());
}
menu.add(1, size, 0, "Configuration");
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
count++;
Toast toast1 = Toast.makeText(this, "Count: "+count, Toast.LENGTH_SHORT);
toast1.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast1.show();
selectedProfileName = null;
//change the map according to the selected profile name
if(item.getItemId()<=profileNames.size()-1){
selectedProfileName = profileNames.get(item.getItemId());
setContentView(new SampleView(this));
}
else{
Intent myIntent=new Intent(this,configurationTabWidget.class);
startActivity(myIntent);
}
//just to re-draw the map with the new selected profile name
return true;
}
You are selecting two items - first you select the "Change Profile" menu item, and then you select the particular item from the SubMenu.
Related
I want to make a navigation drawer which will populate by listview. Now i want that by clicking each item of the list, open a fragment as per my choice. How do I do this?
The NavigationDrawer doesn't use a ListView in the menu, NavigationDrawer uses directly Menu Items it means that in your source code you can add programmatically items to your menu as you need.
Your NavigationDrawer activity or whatever you named
you can add items to the menu using the
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// To Inflate the menu:
// this adds items to the navigation drawer menu if it is present.
getMenuInflater().inflate(R.menu.dashboard, menu);
return true;
}
by default this method create a static menu using a XML layout in the res/menu/menu_file.xml
but if you follow the next source code you will be able to add more items to your navigation drawer programmatically
the Method add() overloads:
add (int titleRes)
add (CharSequence title)
add (int groupId, int itemId, int order, int titleRes);
add (int groupId, int itemId, int order, CharSequence title);
if you already have all the names of your new menu items in an array like itemsName
you can use something like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
for(String itemName : itemsName){
int itemId = Arrays.asList(itemsName).indexOf(itemName);
menu.add(Menu.NONE, itemId, Menu.NONE, itemName);
}
return true;
}
and to manage the click event, on every one, you need to solve it depending on your array of actions to take, in your dynamic array, because you need to know deterministic every action that your menu item need to do or behave, as a menu option.
But in general your click listener for those menu items you use something like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
//here you need the logic of what action
//your item is going to take
//but that is up to you
//how do you want to know what to do
//with a dynamic unknown array of options.
if (itemId == 0){
doSomething();
return true;
}
return false;
}
}
enjoy!!!
I'm trying to create a android menu which should be build dynamically inside a Google Glass app. Therefore I have to arrays which contain the diffent kinds of objects which should be displayed in the menu.
The menu should look like the following:
Menu1
Option1
Option2
Option3
Menu2
Option1
Option2
Menu3
Menu4
I've already build up the menu structure with this code:
public boolean onCreateOptionsMenu(Menu menu) {
menu.clear();
SubMenu damageCodes = menu.addSubMenu(R.string.chooseDamageCode).setIcon(R.drawable.ic_document_50);
int i = 0;
for(Damagecode d : this.mDamagecodes){
damageCodes.add(0, Menu.NONE, i, d.getCotext());
i++;
}
SubMenu priorities = menu.addSubMenu(R.string.choosePriority).setIcon(R.drawable.ic_document_50);
i = 0;
for(Priority p : this.mPriorities){
priorities.add(1, Menu.NONE, i, p.getPriokx());
i++;
}
menu.add(3, Menu.NONE, Menu.NONE, R.string.setConfirmationText).setIcon(R.drawable.ic_pen_50);
menu.add(4, Menu.NONE, Menu.NONE, R.string.backToTplnr_equipment).setIcon(R.drawable.ic_reply_50);
getMenuInflater().inflate(R.menu.create_notification, menu);
return true;
}
I know that the method
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
}
is called when a menuitem is selected but the question right now is how to get the selected item?
Just put a switch inside the onMenuItemSelected:
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch(item.getTitle()) {
case R.string.chooseDamageCode:
// do stuff here for damagecode item
break;
case R.string.choosePriority:
// do stuff here for choosepriority item
break;
...same for other items
}
}
Hope this helps
I found a solution.
Due to the fact that I don't know which elements / values can be in my array I created a simple flag. This means... I'm creating the main menu where every item has a fixed unique ID. The submenu elements do not have a unique id, they only have the title. So what I'm doing right now is to check first whether one of the main menu items was pressed (unique ID) or whether the title of the clicked element is either in one of the arrays or not.
Works quite fine :)
Hope this helps anybody else too!
Greetings
I am trying to display a menu item named "share" by clicking the menu button.
now i need to create a submenu item for this share menu item.
The sub menu item should be visible when the share menu item is clicked alternatively (when clicked first time becomes visible next time invisible and so on.).
I found few solutions which are displaying the sub menuitems but those are being displayed along with the menu item.
This is my code for creating menus
public boolean onCreateOptionsMenu(Menu m) {
m.add(1,1,0,"one").setIcon(R.drawable.icon);
m.add(1,2,0,"two").setIcon(R.drawable.icon);
m.add(1,3,0,"three").setIcon(R.drawable.icon);
m.add(1,4,0,"four").setIcon(R.drawable.icon);
m.getItem(0).setVisible(false);
m.getItem(1).setVisible(false);
return true;
}
and my onPrepareOptionsMenu()
public boolean onPrepareOptionsMenu(Menu m) {
if(isvisible)
{
isvisible = false;
m.getItem(0).setVisible(true);
m.getItem(1).setVisible(true);
}
else{
isvisible = true;
m.getItem(0).setVisible(false);
m.getItem(1).setVisible(false);
}
return super.onPrepareOptionsMenu(m);
}
I need the remaining two items to be shown when i click on the menu item rather than on the menu button.
Can anyone suggest me please
override the below method:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
return super.onPrepareOptionsMenu(menu);
}
The above method is called every time before the menu is shown.
Here you can maintain a bool and hide the submenu depending upon the condition of your check.
EDIT:
By default set bool to false and on false show the menu and set the bool to true and on next call check this bool and if it is true, set it false and make the menu invisible.use this line to make the menu visible or invisible:
menu.getItem(index).getSubMenu().getItem(index).setVisible(true);
Store the bool in sharedpreference.
I wanted to create application where items in list ll be editting when you select item and press in menu "Edit", but items in list dont selecting...
#Override
public boolean onOptionsItemSelected(MenuItem item) {
final long id = this.getSelectedItemId();
switch (item.getItemId()) {
case IDM_EDIT:
if (id > 0) {
CallEditContactDialog(id);
}
else {
Toast.makeText(this, R.string.toast_notify, Toast.LENGTH_SHORT)
.show();
}
break;
}
return(super.onOptionsItemSelected(item));
}
Maybe it is working for android 2nd, but i make it for third too and can test it just on tab 8.9 android 3.2...
Sorry for my english and knowledge in android.
Am i right that you have some items in the list in activity, you want to pres on one of it then press menu button and press Edit ?
I think you have to save id when press on the item in list and then perform change action with this id onOptionsItemSelected...
I have an Activity that has 2 fragments. Both are ListFragments and both contribute MenuItems to the Menu. I have one MenuItem that I've set the attribute android:showAsAction to have it show as a button on the ActionBar. Which works fine.
Now the MenuItem is state dependent. It's a Pause/Resume menu option for pausing and resuming a queue. My problem is I can't figure out how to set it's initial statue when the Fragment is created.
It's state is dependent on the whether the queue is paused or not. So I have an AsyncTask that gets the queue and sets a boolean (paused) based on the state of the queue. I'm calling onPrepareOptionsMenu to set the text for the Pause menu item based on the last known state of the queue and this works great if I leave the MenuItem in the actual menu. You tap the menu icon and onPrepareOptionsMenu is fired and the menu is updated before it's displayed.
The problem is, if I put that same MenuItem on the ActionBar (showAsAction), how can I force it to update without having to call onPrepareOptionsMenu? I need to be able to do this because on first launch of the app, I send a request to get the queue, but the task returns after the ActionBar is setup and displayed. I've created a handler in my fragment that gets called every time I get a queue update, but from there, how can I update the text for my MenuItem on the ActionBar? I can't seem to find a way to get the currently set Menu to manipulate it except for in onPrepareOptionMenu.
Rob W.
Option #1: Try invalidateOptionsMenu(). I don't know if this will force an immediate redraw of the action bar or not.
Option #2: See if getActionView() returns anything for the affected MenuItem. It is possible that showAsAction simply automatically creates action views for you. If so, you can presumably enable/disable that View.
I can't seem to find a way to get the currently set Menu to manipulate it except for in onPrepareOptionMenu.
You can hang onto the Menu object you were handed in onCreateOptionsMenu(). Quoting the docs:
You can safely hold on to menu (and any items created from it), making modifications to it as desired, until the next time onCreateOptionsMenu() is called.
in my case: invalidateOptionsMenu just re-setted the text to the original one,
but directly accessing the menu item and re-writing the desire text worked without problems:
if (mnuTopMenuActionBar_ != null) {
MenuItem mnuPageIndex = mnuTopMenuActionBar_
.findItem(R.id.menu_magazin_pageOfPage_text);
if (mnuPageIndex != null) {
if (getScreenOrientation() == 1) {
mnuPageIndex.setTitle((i + 1) + " von " + pages);
}
else {
mnuPageIndex.setTitle(
(i + 1) + " + " + (i + 2) + " " + " von " + pages);
}
// invalidateOptionsMenu();
}
}
due to the comment below, I was able to access the menu item via the following code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.magazine_swipe_activity, menu);
mnuTopMenuActionBar_ = menu;
return true;
}
To refresh menu from Fragment simply call:
getActivity().invalidateOptionsMenu();
I have used this code:
public boolean onPrepareOptionsMenu (Menu menu) {
MenuInflater inflater = getMenuInflater();
TextView title = (TextView) findViewById(R.id.title);
menu.getItem(0).setTitle(
getString(R.string.payFor) + " " + title.getText().toString());
menu.getItem(1).setTitle(getString(R.string.payFor) + "...");
return true;
}
And worked like a charm to me you can find OnPrepareOptionsMenu here
First please follow the two lines of codes to update the action bar items
before that you should set a condition in oncreateOptionMenu().
For example:
Boolean mISQuizItemSelected = false;
/**
* Called to inflate the action bar menus
*
* #param menu
* the menu
*
* #return true, if successful
*/
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
inflater.inflate(R.menu.menu_demo, menu);
//condition to hide the menus
if (mISQuizItemSelected) {
for (int i = 0; i < menu.size(); i++) {
menu.getItem(i).setVisible(false);
}
}
return super.onCreateOptionsMenu(menu);
}
/**
* Called when the item on the action bar being selected.
*
* #param item
* menuitem being selected
*
* #return true if the menuitem id being selected is matched
* false if none of the menuitems id are matched
*/
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getId() == R.id.action_quiz) {
//to navigate based on the usertype either learner or leo
mISQuizItemSelected = true;
ActionBar actionBar = getActionBar();
invalidateOptionMenu();
}
}
For clarity, I thought that a direct example of grabbing onto a resource can be shown from the following that I think contributes to the response for this question with a quick direct example.
private MenuItem menuItem_;
#Override
public boolean onCreateOptionsMenu(Menu menuF)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_layout, menuF);
menuItem_ = menuF.findItem(R.id.menu_item_identifier);
return true;
}
In this case you hold onto a MenuItem reference at the beginning and then change the state of it (for icon state changes for example) at a later given point in time.