Checking for which submenu user clicked in android - android

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.

Related

How to get all menu items id Android

I can get all the items from the actionbar with getItem() but I need to compare their id's with a particular id. How can I do this?
You can get actionBar menu items id by calling pMenu.getItem();
You can use changing methods of these menu items by overriding the method:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action buttons
switch(item.getItemId()) {
case R.id.action_stats:
break;
}
}

Detect KeyPress on ActionBarSherlock SubMenu

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().

How to distinguish two menu item clicks in ActionBarSherlock?

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.

Can't select item in list

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...

Call listview selected item click, Using Android menu item

In my android application I have a listview with set of records. I have implemented the OnItemClickListener and set it to the listview. So when user click any listview Item it redirects to next activity and do the stuff I need.
The problem is, I have to implement the same functionality to be performed using android Menu.(When click menu button). When click menu button it displays a menu item, to perform Listviews focused item to call click event. Simply when click menu item it should perform the the focused listview item click, as when we normally click the listview item.
Have no idea of how to call click event
Can anyone guide me how to do this please....
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.new_game:
newGame();
return true;
case R.id.help:
showHelp();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Taken from http://developer.android.com/guide/topics/ui/menus.html.

Categories

Resources