Though it's been stated many times it's considered the best programming practice to create UIs in XML rather than in Java, there are still times when using code is the better option. This is especially true if you wanted a menu item to be visible (or enabled) based on some external criteria. Menus can also be included in resource folders, but there are times when you need code to perform the logic of which resource to use. One example might be if you wanted to offer an upload menu item only if the user is logged in to your app.
In this recipe, we will create and modify the menu only through code.