I have a problem in my Android project trying to make a options menu.
When I debug/execute my app and click over the menu-button, ALWAYS R.id.btnInfo attribute returns an integer, not the menu item id (btnInfo).
Here is the code:
Menu declaration:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/btnInfo"
android:title="@string/btnInfo"
android:icon="@drawable/ic_info" />
</menu>
Loading menu:
/**
* Options Menu Inflater Event
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options, menu);
return true;
}
Click Event:
/**
* Click on Options Menu Button
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.btnInfo:
// To-do:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
This is the correct behaviour.
Check the
R.javafile, for each item you give an ID, it generates an integer ID to refer too.Example: