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>