I am trying to implement a custom overflow menu in a toolbar, the image is attached for reference. I went through many tutorials and articles to implement the desired result but was unable to do it. I managed to implement it a little bit but was unable to customize it more. I read a few articles which suggested using ListPopupWindow or PopupMenu can anyone guide me or share any tutorials or articles?
I need the following to be done:
- Layout width to wrap_content
- Show the selected item background, icon and text color
- Layout to be shown just below the toolbar not on the toolbar


I managed to get the desired result using ListPopupWindow and code is below.
NOTE
Step 1
Create a custom layout for a single item which will appear in ListPopupWindow.
item_overflow.xml
Step 2
Create a custom adapter for a single item which will appear in ListPopupWindow.
OverflowMenuAdapter.kt
Step 3
Create two drawables files. One for the ListPopupWindow which will customise its background like its radius, color, padding and etc. Second for the background of an item of ListPopupWindow when it is selected/clicked like its radius, color and etc.
background_overflow.xml
background_selected_overflow.xml
Step 4
Create ListPopupWindow instance and show when overflow icon is clicked on Toolbar
MainActivity.kt
I hope it helps someone out.