MENU

Functions for adding menu

  • onCreateOptionsMenu(Menu menu)
  • onOptionsItemSelected(MenuItem item)

Explicit way

In onCreateOptionsMenu() , add the items as :

menu.add(GroupId,itemId,order,name);

In onOptionsItemSelected() , write the following code :

int id= item.getItemId();
switch(id){

case 0:
//do the code here

case 1:
//do the code here
}

Implicit way

Create an xml file for menu. Add items in that menu resource file.

And in onCreateOptionsMenu(), write code as :


MenuInflator inflator = getMenuInflator();
inflator.inflate(R.menu.file , menu );

Inflating a view means taking the xml file and creating the actual objects so that they are actually visible on Activity screen.

Leave a comment