Implement Pop Up Menu With Margin
Solution 1:
You can change your PopupMenu's position by using the following attributes: gravity, dropDownHorizontalOffset and dropDownVerticalOffset
First set gravity to Gravity.END
popup.setGravity(Gravity.END);
Then change your dropdown-offsets by creating a style
<stylename="MyPopupMenu"parent="@style/Widget.AppCompat.PopupMenu"><itemname="android:dropDownHorizontalOffset">-4dp</item><itemname="android:dropDownVerticalOffset">4dp</item></style>
If you want to overlap the anchor view use
parent="@style/Widget.AppCompat.PopupMenu.Overflow"
Lastly apply MyPopupMenu to your theme
<itemname="popupMenuStyle">@style/MyPopupMenu</item>
Solution 2:
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 about ListPopupWindow
. 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:
publicclassMainActivityextendsAppCompatActivity
{
private ImageButton mMoreOptionsButton;
private ArrayAdapter<String> mPopupAdapter;
private ArrayList<String> mOptionsArray =
newArrayList<>(Arrays.asList("Option1", "Option2", "Option3"));
private ListPopupWindow mPopupWindow;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMoreOptionsButton = (ImageButton) view.findViewById(R.id.more_options_button);
setupPopupWindow();
}
privatevoidsetupPopupWindow()
{
mPopupAdapter = newArrayAdapter<>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, mOptionsArray);
mPopupWindow = newListPopupWindow(MainActivity.this);
mPopupWindow.setAdapter(mPopupAdapter);
mPopupWindow.setAnchorView(mMoreOptionsButton);
mPopupWindow.setWidth(500);
mPopupWindow.setHorizontalOffset(-380); //<--this provides the margin you need//if you need a custom background color for the popup window, use this line:
mPopupWindow.setBackgroundDrawable(newColorDrawable(ContextCompat.getColor(MainActivity.this, R.color.gray)));
mPopupWindow.setOnItemClickListener(newAdapterView.OnItemClickListener() {
@OverridepublicvoidonItemClick(AdapterView<?> parent, View view, int position, long id)
{
//do something with item by referring to it using the "position" parameter
}
});
mMoreOptionsButton.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v)
{
mPopupWindow.show();
}
});
}
}
The key part is calling mPopupWindow.setHorizontalOffset()
. Be aware that this method is tricky. Depending on the value you set in mPopupWindow.setWidth()
you will have to adjust the value in setHorizontalOffset()
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()
and setVerticalOffset()
if you want some margin at the top of your popup window.
Hope this helps :]
Solution 3:
You can set popUpWindow at particular location
popupWindow .showAtLocation(popupView, Gravity.CENTER, 0, 0);
publicvoidshowAtLocation(View parent, int gravity, int x, int y){
showAtLocation(parent.getWindowToken(), gravity, x, y);
}
If you want to show it as DropDown then you can try
publicvoidshowAsDropDown(View anchor, int xoff, int yoff){
showAsDropDown(anchor, xoff, yoff, DEFAULT_ANCHORED_GRAVITY);
}
Take a look on Documentation of PopupWindow http://developer.android.com/intl/es/reference/android/widget/PopupWindow.html
Solution 4:
PopupMenupopup=null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
popup = newPopupMenu(context, anchorView, Gravity.END, 0, R.style.MyPopupMenu);
}else{
popup = newPopupMenu(context, anchorView);
}
style
<stylename="MyPopupMenu"parent="@style/Widget.AppCompat.PopupMenu"><itemname="android:dropDownHorizontalOffset">-4dp</item><itemname="android:dropDownVerticalOffset">4dp</item></style>
Solution 5:
Just declare your popup menu like this var popup = PopupMenu(this.requireContext(), isw_settings_icon, Gravity.END, 0, R.style.yourmenustyle)
Post a Comment for "Implement Pop Up Menu With Margin"