Skip to main content

Posts

Showing posts from November, 2014

Create Android Options Menu Programatically

In this post we are going to learn about how to show an android menu without using a separate menu layout xml file. You can create a menu in two ways. 1. Create the menu by inflate an xml file located in the menu folder using the MenuInflater object. 2. Add the menu items dynamically (by writing code) to menu using the add() method. Here I demonstrate how to add the menu items dynamically to an options menu. Step 1:  Override the onCreateOptionsMenu in the MainActivity.java file. @Override public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub } Ads by Google Step 2: Add each menu items to the menu using the add() method and the Menu object.             add(arg1, arg2, arg3, arg4);             The add method takes three arguments.             arg1   =  Indicates the group id . You can pass Menu.NONE if there is no group available.             arg2  =   Indicates the item id.              arg 3 = I