0

this is probably a simple question for most of you but I'm just wondering if any of you could tell me what exactly MenuItem item is targeting when working with toolbars? I'm taking a class on Android at College and the objective is to make a simple menu, but for some reason I can't get my menu items to be targeted even when assigning an id to the menu layout. Anyone know what I'm missing here?

onOptionsItemSelected code

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        String message = null;
        //Look at your menu XML file. Put a case for every id in that file:
        switch(item.getItemId())
        {
            //what to do when the menu item is selected:
            case R.id.rockstar:
                message = "You clicked item 1";
                break;
            case R.id.motorcycle:
                message = "You clicked on item 2";
                break;

        }
        Toast.makeText(this, message, Toast.LENGTH_LONG).show();
        return true;
    }

Menu layout code

    <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/MenuItem">
    <item android:id="@+id/rockstar"
        android:title="MENU_ITEM 1"
        android:icon="@drawable/rockstar"
        android:orderInCategory="10"
        app:showAsAction="always"/>  <!-- This uses showAsAction=always, so look at icon= parameter -->


    <item android:id="@+id/motorcycle"
        android:orderInCategory="50"
        app:actionViewClass="android.widget.SearchView"
        android:icon="@drawable/motorcycle"
        app:showAsAction="always|collapseActionView"
        android:title="MENU_ITEM_2"/>

</menu>
  • Do you also call [`onCreateOptionsMenu`](https://developer.android.com/guide/topics/ui/menus#options-menu) somewhere too? – Tyler V Jul 03 '22 at 03:54
  • I'm not even able to get to that stage, I'm getting an error message saying "Cannot Resolve Symbol 'item' when I write it out. – ReginaldRey Jul 03 '22 at 04:49

1 Answers1

0

To add a menu to an Activity you need to do three things:

1. Make a Menu XML file

The XML file describes what items will be in the menu. Make sure this is in the res/menu folder and not res/layout. To create a new menu XML file in Android Studio, right click on the "res" folder, create new "Android Resource File" and choose "Menu" in the "Resource Type" dropdown. If this file is in res/layout it will not be able to resolve the item XML entries.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item android:id="@+id/rockstar"
        android:title="@string/rockstar"
        android:icon="@drawable/ic_baseline_add_location_24"
        android:orderInCategory="1"
        app:showAsAction="always"/>

    <item android:id="@+id/motorcycle"
        android:orderInCategory="2"
        android:icon="@drawable/ic_baseline_attachment_24"
        app:showAsAction="always"
        android:title="@string/motorcycle"/>

</menu>

2. Create the Menu

To actually create the menu, you need to override onCreateOptionsMenu in your activity. This is where you tell it which menu XML file to use to build the menu. The call to inflate takes the name of the XML file from Step 1 (res/menu/my_menu.xml here) and uses it to actually build the menu views.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.my_menu, menu);
    return true;
}

3. Respond to Menu actions

Finally, to respond to the user clicking on things in the menu you need to override onOptionsItemSelected in your activity. Note that due to resource IDs being non-final there is conflicting information on whether it is good to use a switch-case in here. The official menu docs still do, but it is probably best to heed the warnings and use an if-else instead (but realistically for most projects it won't make a difference either way). Remember to return super.onOptionsItemSelected(item) when the ID is not one from your menu so the parent views can handle it.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    String message;
    int itemId = item.getItemId();
    
    if( itemId == R.id.rockstar ) {
        message = "You clicked item 1";
    }
    else if( itemId == R.id.motorcycle) {
        message = "You clicked on item 2";
    }
    else {
        return super.onOptionsItemSelected(item);
    }
    
    Toast.makeText(this, message, Toast.LENGTH_LONG).show();
    return true;
}

If symbols can't be resolved, make sure you have the right imports for all the menu items - e.g.

import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
Tyler V
  • 9,694
  • 3
  • 26
  • 52
  • i don't know why if we put the codes of menu xml file and paste them in another xml file in layout folder, it would not resolve and i tag – Long tran Feb 24 '23 at 14:25