I am using the default pop menu and expecting the behavior of the same. Everything is working fine. What my concern is regarding the rendering of pop up menu. My pop up menu sticks to the right of the screen.
I want the behavior as used by Youtube app for android.
I am mainly not able to provide right margin to my pop up menu. Please help. I have tried providing Gravity to PopUp Menu. But Pop Up Menu sticks to the Right of screen.
PopupMenu popupMenu = new PopupMenu(mContext, anchor, Gravity.LEFT);
popupMenu.getMenuInflater().inflate(R.menu.menu_edit_accessory, popupMenu.getMenu());
This is a little late but I hope this helps someone.
I was trying to do what you were doing with a
PopupMenu
but nothing was working for me until I learned aboutListPopupWindow
. It's a way better alternative. Much more flexible in my opinion and you can achieve the margin-spacing you were asking about.Here's the code:
The key part is calling
mPopupWindow.setHorizontalOffset()
. Be aware that this method is tricky. Depending on the value you set inmPopupWindow.setWidth()
you will have to adjust the value insetHorizontalOffset()
accordingly. It just so happened that for my app,-380
was the perfect amount of margin I needed from the end. So you may have to play with this value a bit.I believe the same can be said for using
setHeight()
andsetVerticalOffset()
if you want some margin at the top of your popup window.Hope this helps :]
You can set popUpWindow at particular location
If you want to show it as DropDown then you can try
Take a look on Documentation of PopupWindow http://developer.android.com/intl/es/reference/android/widget/PopupWindow.html
You can change your PopupMenu's position by using the following attributes: gravity, dropDownHorizontalOffset and dropDownVerticalOffset
First set gravity to Gravity.END
Then change your dropdown-offsets by creating a style
If you want to overlap the anchor view use
Lastly apply MyPopupMenu to your theme