Modifying menus and menu items during runtime
Though it's been stated many times, it's considered the "best" programming practice to create UI in XML rather than in Java. There are still times when you may need to do it in code. 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. 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.
Create a new project in Android Studio and call it
RuntimeMenu using the default Phone & Tablet option. Select the