Android – Correct use of invalidateOptionsMenu()

invalidateOptionsMenu() is used to say Android, that contents of menu have changed, and menu should be redrawn. For example, you click a button which adds another menu item at runtime, or hides menu items group. In this case you should call invalidateOptionsMenu(), so that the system could redraw it on UI. This method is a … Read more

Toolbar options menu background color

To change the toolbar options menu color, add this to your toolbar element app:popupTheme=”@style/MyDarkToolbarStyle” Then in your styles.xml define the popup menu style <style name=”MyDarkToolbarStyle” parent=”ThemeOverlay.AppCompat.Light”> <item name=”android:colorBackground”>@color/mtrl_white_100</item> <item name=”android:textColor”>@color/mtrl_light_blue_900</item> </style> Note that you need to use colorBackground not background. The latter would be applied to everything (the menu itself and each menu item), the … Read more

Clicking hamburger icon on Toolbar does not open Navigation Drawer

You’re using the four-parameter constructor for ActionBarDrawerToggle, which means you’ll have to call the toggle’s onOptionsItemSelected() method in MainActivity‘s onOptionsItemSelected() override in order to open/close the drawer. For example: @Override public boolean onOptionsItemSelected(MenuItem item) { if(mDrawerToggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); } If you happen to be providing your own Toolbar – e.g., as … Read more

How to add Options Menu to Fragment in Android

Call the super method: Java: @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { // TODO Add your menu entries here super.onCreateOptionsMenu(menu, inflater); } Kotlin: override fun void onCreate(savedInstanceState: Bundle) { super.onCreate(savedInstanceState) setHasOptionsMenu(true) } override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { // TODO Add your menu entries … Read more