(Newbe)
When I click on a menu the above method returns an ID from the first menu, not the one I clicked. If I check for the Title Condensed of the menu it is correct.
int id = item.getItemId(); //returns id of an incorrect menu
String Title = (String) item.getTitleCondensed(); //this returns the correct title.
Any ideas welcome.
I had the same problem. Generated files from the build are not properly updated.
I got the same effect if i reordered the menu items in the xml...build and surprise. Clicking on menu brings other codes than expected.
Do a clean and try again
You should have set each menu item a unique ID in onCreateOptionsMenu and onCreateContextMenu.
For example:
public static final int CONTEXT_MENU_DELETE = Menu.FIRST;
public static final int CONTEXT_MENU_EDIT = CONTEXT_MENU_DELETE + 1;
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.add(0, CONTEXT_MENU_DELETE, 1, R.string.delete);
menu.add(0, CONTEXT_MENU_EDIT, 2, R.string.edit);
}
// And then
#Override
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()) {
case CONTEXT_MENU_DELETE:
// Delete item
break;
case CONTEXT_MENU_EDIT:
// Edit item
break;
}
}
The same is for onCreateOptionsMenu and onOptionsItemSelected. You should have a unique constant for every menu option.
Added:
Didn't you check out this tutorial?
The idea is the same. You should set different ids in menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/new_game"
android:icon="#drawable/ic_new_game"
android:title="#string/new_game" />
<item android:id="#+id/help"
android:icon="#drawable/ic_help"
android:title="#string/help" />
</menu>
And then use those ids in onOptionsItemSelected:
#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);
}
}
Check out these code blocks from official Android Menu tutorial and compare to your own code. You could also publish your menu.xml, onCreateOptionsMenu and onOptionsItemSelected so it would be easy to figure out your problem.
Related
I have a menu with a submenu. The parent item is "Options" and its children are "Call", "Navigate", and "Edit". Here is my activity code that handles the menu click. The problem is that the first submenu always gets selected by the event. With this code if i click on "Call"(the first child item) the onOptionsItemSelected() method registers the R.id.record_edit item being selected. I don't know why this is happening since the "Call" menu item has the id of "record_call". I've tried switching the submenu items around in the xml but the first submenu item is the only one that registers and it always registers as R.id.record_edit. Not sure what's wrong with my code.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.record_edit:
Intent intent = new Intent(this, EditRecordActivity.class);
myApp.setRecord(record);
Bundle b = new Bundle();
b.putBoolean("new", false);
b.putString("pageTitle", pageTitle);
b.putString("meta_universalid", meta_universalid);
intent.putExtras(b);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
xml menu
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/record_options"
android:showAsAction="ifRoom|withText"
android:title="..."
android:titleCondensed="Options"
android:enabled="true">
<menu>
<item
android:id="#+id/record_call"
android:showAsAction="ifRoom|withText"
android:title="#string/record_call"/>
<item
android:id="#+id/record_navigate"
android:showAsAction="ifRoom|withText"
android:title="#string/record_navigate"/>
<item
android:id="#+id/record_edit"
android:showAsAction="ifRoom|withText"
android:title="#string/record_edit"/>
</menu>
</item>
</menu>
just checked with your menu to be sure. It is working fine with me. Check whether your overall format is like the following code
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.record_call:
Toast.makeText(this, "call", 1000).show();
return true;
case R.id.record_edit:
Toast.makeText(this, "edit", 1000).show();
return true;
case R.id.record_navigate:
Toast.makeText(this, "navigate", 1000).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I want to have on menu button click to call a function,
that function will popup a window with place for input.
I added to the activity_main.xml the following line
android:onClick="getURL"
and have defined a function in my mainActivity.java:
public void getURL(View view) {...}
however, when i load the app i get:
01-31 09:59:13.479: E/AndroidRuntime(761): FATAL EXCEPTION: main
01-31 09:59:13.479: E/AndroidRuntime(761): android.view.InflateException: Couldn't resolve menu item onClick handler getURL in class com.example.remoteswitch.MainActivity
my menu settings looks like:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/menu_settings"
android:onClick="getURL"/>
any ideas why it happens to me?
The way you handle the click on a Menu Item is like this:
First, setup your Options Menu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater mnuInflater = getSupportMenuInflater();
mnuInflater.inflate(R.menu.your_menu_xml, menu);
return true;
}
Handle the clicks here:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_settings:
// EITHER CALL THE METHOD HERE OR DO THE FUNCTION DIRECTLY
yourMethod();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
And do the function in the yourMethod() here:
private void yourMethod() {
// TODO Auto-generated method stub
}
I am not sure if the onClick method is valid / works when it comes to an Options Menu. The above code works as it should anyway.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menuxml_name, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId())
{
case R.id.menu_settings:
getURL();
break;
}
return super.onOptionsItemSelected(item);
}
Try these override methods into activity so that it can be easy to perform your operation
Just to add something to an old post:
First of all, the parameter of your method should have been MenuItem not View (Bet you already knew that).Secondly, from the official google developer website, it says:
Tip: Android 3.0 adds the ability for you to define the on-click behavior for a menu item in XML, using the android:onClick attribute. The value for the attribute must be the name of a method defined by the activity using the menu. The method must be public and accept a single MenuItem parameter—when the system calls this method, it passes the menu item selected. For more information and an example, see the Menu Resource document.
To use this option, define a method (the method you want to call when the options menu item is clicked) in your activity.For example:
public void showOptionMenuItem(MenuItem item) {
Toast.makeText(getApplicationContext(),
"You have selected the GET URL option", Toast.LENGTH_LONG).show();
}
and then in your XML, you do this:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
...
<item android:id="#+id/getUrl"
android:onClick="getURL" //<----
android:title="#string/get_url"/>
</menu>
I use an xml to for my ContextMenu, which is like :
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/Ordermenu" android:title="Order">
<menu android:id="#+id/OrderBySubMenu">
<item android:id="#+id/OrderByASC" android:title="Order ASC" />
<item android:id="#+id/OrderByDESC" android:title="Order DESC" />
<item android:id="#+id/Cancel" android:title="Cancel" />
</menu>
</item>
<item android:id="#+id/ActionAmenu" android:title="Action A"/>
</menu>
I use following code to display the menu, in my onCreateContextMenu
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.my menu, menu);
I manage option click with following code :
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.Displaymenu:
//do stuff
return true;
case R.id.OrderByASC:
//do stuff
return true;
case R.id.OrderByDESC:
//do stuff
return true;
default :
return(super.onOptionsItemSelected(item));
}
Starting the Context Menu it display Two options:
Order
Action A
Clicking on Order show a submenu :
Order ASC
Order DESC
Cancel
Now, If the user click on cancel (or click on the hardware back button), no action is specified, so it call super.onOptionsItemSelected(item) which go back to my main activity.
How can I manage to go back to the main menu in such case? i.e. diplay the initial :
Order
Action A
I tried this long ago but i think you will have to override onPrepareOptionsMenu as well to get this to work. This is called before it shows, and you will have to put flags here on what items to show for the user.
Try something like this:
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
// Clear the previous layout
menu.clear();
if(showMainMenu)
{
// Add main menu items..
menu.add(0, R.id.ordermenu, 0, "True");
}
else
{
// Add sub-menu items
menu.add(0, R.id.ordermenuASC, 0, "True");
}
return super.onPrepareOptionsMenu(menu);
}
So when user clicks a main menu item, change the boolean flag a redo the process.
Finally, it worked only by adding :
case R.id.Cancel:
openContextMenu(findViewById(selected_view_id));
return true;
in public boolean onContextItemSelected(MenuItem item)
selected_view_id is stored by
selected_view_id=v.getId();
in onCreateContextMenu
Hope it will help others.
I have menu in my App. I need to check for the which menu item been selected by the user and take appropriate action. I did this as,
#Override
public boolean onOptionsItemSelected(MenuItem menuitem){
String title = menuitem.getTitle().toString();
int itemId = menuitem.getItemId();
switch(itemId){
case 2131296257:
-----------------;
break;
case 2131296258:
------------------;
break;
}
But these MenuItem Id's are getting changed each time I run my App. Now I thought to compare the menuTitle with hard coded string values like ,
String title = menuitem.getTitle().toString();
if(title.equals("Settings..")){
-------------
-------------
-------------
}
But I don't think it's a good practice. But I guess I can do the same using the menu titles defined in strings.xml. But I am not sure how to use this.... (like R.string.....).
Can someone suggest on this please.
Thanks
Here's an example menu named game_menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/new_game"
android:icon="#drawable/ic_new_game"
android:title="#string/new_game" />
<item android:id="#+id/help"
android:icon="#drawable/ic_help"
android:title="#string/help" />
</menu>
Inflating a Menu Resource
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.game_menu, menu);
return true;
}
Responding to user action
#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);
}
}
for more information click here
I'm new to this forum and to android development itself so my question will probably be a very stupid one and i apologize for this .
I began reading the Dev Guide on developer.android.com and until the part with the context menus everything worked pretty fine.
Now i tried to have a context menu with a submenu that contains some checkable items. So i added the submenu and the items to my menu.xml and some item.setchecked(true) methods to my onContextItemSelected(...) method.
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/info"
android:title="#string/info" />
<item android:title="#string/change">
<menu>
<item android:id="#+id/checkable_item1"
android:checked="true"
android:checkable="true"
android:title="#string/hello"/>
<item android:id="#+id/checkable_item2"
android:checkable="true"
android:title="#string/moin"/>
<item android:id="#+id/checkable_item3"
android:checkable="true"
android:title="#string/aloha"/>
</menu>
</item>
</menu>
part of my .java file
...
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater1 = getMenuInflater();
inflater1.inflate(R.menu.context_menu, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case R.id.checkable_item1:
if(item.isChecked()) item.setChecked(false);
else item.setChecked(true);
return true;
case R.id.checkable_item2:
if(item.isChecked()) item.setChecked(false);
else item.setChecked(true);
return true;
case R.id.checkable_item3:
if(item.isChecked()) item.setChecked(false);
else item.setChecked(true);
return true;
default:
return super.onContextItemSelected(item);
}
}
...
Now the problem is that when i open the menu and press one of the checkable items and i can see that the green tick pops up in the little box just before the context menu closes but when i open up the menu again the tick is gone.
Now i don't really know why the tick doesn't stay in the box.
It would be nice if someone could give me hint and tell me what i'm doing wrong.
thanking you in anticipation
jean-claude91
I haven't tried it myself but if I read the description here properly (http://developer.android.com/reference/android/app/Activity.html#onCreateContextMenu(android.view.ContextMenu, android.view.View, android.view.ContextMenu.ContextMenuInfo)),
your layout resource will be called every time the menu is created. Since it's "not safe to hold on to a menu after the method returns" , you would need to process the selected item and persist that selection somewhere and then pass the current state of the selectable items into the onCreate with menuInfo, setting checked/unchecked using that info.
If you don't, then the menu will be recreated every time based on your default settings (menu.xml).