I have some XML that I am using as an Android text menu.
Here is the XML
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_settings"
android:title="@string/menu_settings"
android:orderInCategory="100"
android:showAsAction="never" />
<item android:id="@+id/menu_search" android:title="@string/menu_search"></item>
<item android:id="@+id/exit" android:title="@string/menu_exit" android:orderInCategory="200" ></item>
</menu>
In inflate the menu using the following code
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
When I am checking for the menu items clicked, I use this.
public boolean onOptionsItemSelected(MenuItem item) {
int itemid = item.getItemId();
Toast.makeText(this, "You pressed the icon!" + itemid, itemid).show();
return true;
}
This is giving me a numeric ID for each menu item pressed. Should I use a case statement to check this and then launch the pages I want to open? Or is there a way for me to link an intent to a menu item ? Its seems like a bit of a waste to be adding this code to each screen.
You should use case statment for calling :-