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)
Related
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 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 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.
I have created a submenu using the manifest file. I have four text fields in my submenu. I want to know which submenu item the user clicked. I know about the switch case item.getItemId condition for the menu items. But i want to know how to check for which submenu the user clicked.
Thanks for any help
You could save the id of the submenu item to a variable. For example:
int item;
#Override
public void onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.something: {
this.item = something;
return true;
}
}
Now the variable item contains the id of the item selected.
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.