-1

I came across the code here. Can someone explain to what is Menu.First and why +1? I couldn't find it in the Android developer document.

    private static final int MENU_ADD = Menu.FIRST;
    private static final int MENU_LIST = MENU.FIRST + 1;
    private static final int MENU_REFRESH = MENU.FIRST + 2;
    private static final int MENU_LOGIN = MENU.FIRST + 3;
Spotty
  • 197
  • 2
  • 2
  • 14
  • Where is this code from...? If you look at the source code for [android.view.Menu](http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/view/Menu.java), then you will find `Menu.FIRST`, which is simply a [constant](http://www.java2s.com/Tutorial/Java/0020__Language/Defineconstant.htm) value set to `1`. However, the rest of the values (`MENU_LIST` etc...) aren't there. – PPartisan Jan 06 '16 at 14:39
  • http://stackoverflow.com/questions/15580111/how-can-i-dynamically-create-menu-items from here – Spotty Jan 06 '16 at 14:40

2 Answers2

0

Refer this documentation

FIRST Refers to

First value for group and item identifier integers.

It is a constant value

Constant Value: 1 (0x00000001)

Refer this link

Viral Patel
  • 32,418
  • 18
  • 82
  • 110
  • :( sorry could you explain what that mean?? I don't quite get it. – Spotty Jan 06 '16 at 14:54
  • It is basically just a constant with value 1 which can be used for constructing menu groups. Read a very nice overvew of Menus and the usage of such constants here: http://gurushya.com/android-menus-part-1/ – Viral Patel Jan 06 '16 at 15:00
0

In the context of the accepted answer referenced in your comments, the poster is simply using these values as id's for dynamically added menu items so that clicks can be registered in inOptionsItemSelected().

For example, say you wanted to add a new button to your menu dynamically that turned the screen blue, you might create a constant value called MENU_TURN_SCREEN_BLUE. This would store an arbitrary number which you can later use as an id. For example (keeping in mind Menu.FIRST = 1:

private static final int MENU_TURN_SCREEN_BLUE = Menu.FIRST + 60;

or

private static final int MENU_TURN_SCREEN_BLUE = 69084;

Are both valid. Now, when you add a new item to a menu with the add() method, you can use this value:

menu.add(0, TURN_SCREEN_BLUE, 0, "Press To Turn Screen Blue");

You could of course just write the number in directly, but constant are helpful (amongst other reasons) for avoiding bugs in your code that arise from accidentally typing in the wrong number.

Keep in mind though that there are other ways to generate unique id's - see this question for details.

Community
  • 1
  • 1
PPartisan
  • 8,173
  • 4
  • 29
  • 48
  • Then will it be possible to set id for such a code menu.getItem(0).setIcon(R.drawable.bluetooth); on onPrepareOptionsMenu so that I can used it on onOptionsItemSelected() – Spotty Jan 06 '16 at 15:41
  • @Spotty `getItem()` uses the index (i.e., its position in the menu), to find the item, not the view's `id`, so they aren't the same thing. `menu.findItem(TURN_SCREEN_BLUE).setIcon()` should work though. – PPartisan Jan 06 '16 at 15:46
  • Hi I mean something like this question I just posted http://stackoverflow.com/questions/34637751/how-do-i-set-another-id-for-the-new-icon-i-set-in-onprepareoptionsmenu – Spotty Jan 06 '16 at 16:19
  • @Spotty That's a very different question to the one you asked. You asked what is `Menu.FIRST` and received two accurate answers. Please accept one of them at least, otherwise you're wasting the time of people who answer your questions. – PPartisan Jan 06 '16 at 16:37