unairoldan
unairoldan

Reputation: 2863

Android MenuItem.getItemId returns integer values

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);
   }
 }

Upvotes: 0

Views: 2033

Answers (1)

Ricky
Ricky

Reputation: 7889

This is the correct behaviour.

Check the R.java file, for each item you give an ID, it generates an integer ID to refer too.

Example:

public static final class menu {
    public static final int option1=0x7f0a0000;
    public static final int option2=0x7f0a0001;
    public static final int option3=0x7f0a0002;
}

Upvotes: 2

Related Questions