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().
Related
Here is the screen shot of my sidebar navigation bar.
I want to make the items in the sidebar navigation Menu clickable so that I can move to another Activity how can I do this?
You have to treat these as Menu items.
When you set up the navigation bar from the layout XML you attach a menu.xml to it which contains menu items that are then displayed in the navigation bar.
Method 1:
Now from the activity where you have your navigation bar variable you need to get the menu from the navigation view object.
Menu menu = navigation_view.getMenu();
Then you can get individual menuItems from this menu object.
MenuItem myAccount = menu.getItem(0);
MenuItem settingsItem = menu.getItem(1);
MenuItem logoutItem = menu.getItem(2);
Now you can use each of this menuItem objects to set menu click listeners on them and then start the next activity from there. e.g.
myAccount.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
Intent intent = new Intent(context, ToAcitivity.class);
startActivity(intent);
return true;
}
});
Similarly, you can use rest of the objects and make them clickable.
Method 2:
You can also use
navigation_view.setNavigationItemSelectedListener(new
NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case 1:
break;
case 2:
}
return false;
}
});
Second one being more simpler to use
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
Is there any way to use the standard menu button in the action bar to do something? In my case a sliding menu, I already have code that works in the onClick of a normal button. I just want to use the menu button in the action bar instead. Is this possible? Or will I have to customize the action bar and not use the button that is already there?
I believe you mean the ActionBar Home icon.
First you have to enable it
getActionBar().setHomeButtonEnabled(true);
Then you have to handle the event
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case android.R.id.home:
//do your work
return true;
default:
return super.onOptionsItemSelected(item);
}
}
FYI if you want to follow the Navigation drawer pattern you should read this tutorial.
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 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.