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
Related
I want to give Option menu in my application and also that menu are clickable. If I click any menu than it will open another activity. So, please help me for this. In my application there is many class or activity i want to put this optionmenu in every activity of my application. here i try with this kind of code.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuItem dashboard = menu.add(0, 1, 1, "Dashboard");
MenuItem roles = menu.add(0, 2, 2, "Roles");
MenuItem profiles = menu.add(0, 3, 3, "Profiles");
MenuItem move = menu.add(0, 4, 4, "Move Product");
MenuItem assignedproduct = menu.add(0, 5, 5, "Assigned Product Report");
MenuItem salesreport = menu.add(0, 6, 6, "Sales Report");
MenuItem salesreturn = menu.add(0, 7, 7, "Sales Return");
MenuItem purchasereport = menu.add(0, 8,8, "Purchase Report");
MenuItem logout = menu.add(0, 9, 9, "Logout");
super.onCreateOptionsMenu(menu);
return true;
You need to override onCreateOptionsMenu function in each activity of your app. that's the only way
To give the click functionality to menu items, you will have to override the onOptionsItemSelected() method. Then give those MenuItems their respective action based on their item id as follows...
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1:
// give action to the menu item which id is 1
return true;
case 2:
// give action to the menu item which id is 2
return true;
case 3:
// give action to the menu item which id is 3
return true;
case 4:
// give action to the menu item which id is 4
return true;
..........
..........
default:
return super.onOptionsItemSelected(item);
}
}
If all of your activities should have same menu, best way is move creating menu in superclass. . Name it MenuActivity, for example. In this class override onCreateOptionsMenu for creating menu and onOptionsItemSelected for handling user taps. Then just inherit all your activities from MenuActivity.
I'm using com.actionbarsherlock.view.ActionMode and when I try to get the clicked item id like this:
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Toast.makeText(WeiboActivity.this, "Got click: " + item.getItemId(), Toast.LENGTH_SHORT).show();
return false;
}
}
it's always:
Got click: 0
So what is the right code to get the clicked item id?
You need to include item id in your call when you created the menu item
menu.add(Menu.NONE, R.id.menu_test, Menu.NONE, R.string.menu_test)
How do I detect keypress and which key user pressed on SubMenu? [the one on the actionbar where user press and a long list would drop down]
SubMenu subMenu1 = menu.addSubMenu("Option");
subMenu1.add("Comments");
subMenu1.add("More screens");
subMenu1.add("Copy Website URL");
subMenu1.add("Go to Website");
MenuItem subMenu1Item = subMenu1.getItem();
subMenu1Item.setIcon(R.drawable.icon_share);
subMenu1Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
break;
}
return true;
}
I suppose you mean "which item a user selects" in the menu, and not "which key a user pressed". You can provide the action in the onOptionsItemSelected() method that you already have. But before, you have to...
Either change the way that you programmatically add items to your subMenu a little, following this solution: https://stackoverflow.com/a/9080046/1140682
Or define your menu and submenu in an XML file and use the MenuInflater to add the items to your Activity.
Finally, just use the itemId parameter from the add() method (first solution) or the android:id from the XML (second solution) to decide on an action in the switch statement of onOptionsItemSelected().
I have been working with ActionBarSherlock recently, and follwing various tutorials, I wrote this code to add items to Action bar
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("Refresh")
.setIcon(R.drawable.ic_action_refresh)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add("Search")// Search
.setIcon(R.drawable.ic_action_search)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}
However, I dont know how to distinguish the two clicks.
Although I did find out that you have to Override onOptionsItemSelected to handle the clicks and also that a switch statement can be used to distinguish between clicks, but most tutorials use item ids from thier xml menus. Since I am not creating menus in xml how can i distinguish the clicks without ids.
private static final int REFRESH = 1;
private static final int SEARCH = 2;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, REFRESH, 0, "Refresh")
.setIcon(R.drawable.ic_action_refresh)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(0, SEARCH, 0, "Search")
.setIcon(R.drawable.ic_action_search)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case REFRESH:
// Do refresh
return true;
case SEARCH:
// Do search
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Just check following
http://developer.android.com/guide/topics/ui/actionbar.html
which contains
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) { <--- here you can get it
case android.R.id.home:
// app icon in action bar clicked; go home
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
you can do ti by there Id in onOptionsItemSelected ................which can be set here also
http://thedevelopersinfo.wordpress.com/2009/10/29/handling-options-menu-item-selections-in-android/
http://developer.android.com/reference/android/view/Menu.html#add(int, int, int, java.lang.CharSequence)
use
public abstract MenuItem add (int groupId, int itemId, int order, CharSequence title)
Since: API Level 1
Add a new item to the menu. This item displays the given title for its label.
Parameters
groupId The group identifier that this item should be part of. This can be used to define groups of items for batch state changes. Normally use NONE if an item should not be in a group.
itemId Unique item ID. Use NONE if you do not need a unique ID.
order The order for the item. Use NONE if you do not care about the order. See getOrder().
title The text to display for the item.
Returns
The newly added menu item.
I have a Gallery view, In this view I set up an options menu
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, EMAIL_MENU_ID, 0, "Email");
menu.add(0, SHARE_MENU_ID, 0, "Share");
menu.add(0, RATE_MENU_ID, 0, "Rate");
menu.add(0, BUY_MENU_ID, 0, "Buy");
return true;
}
I have the following method to get the option item selected.
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case EMAIL_MENU_ID:
sendMail(index);
case SHARE_MENU_ID:
postToWeb(index);
case RATE_MENU_ID:
postRating(index);
case BUY_MENU_ID:
buy(index);
}
return super.onOptionsItemSelected(item);
}
How could i find the item of the Gallery view that's currently in focus?
Very good tutorial. In my scenario the user has pressed the Menu button and I would like to present the user with some options to take on the image that is currently in focus. At this point the user has not clicked on the image so I don't have the position that you would normally get when a user selects an item. I've thought about using a context menu instead but was hoping to use an options menu.