In Android API 19 was added contructor of PopupMenu which allows specify gravity.
public PopupMenu (Context context, View anchor, int gravity)
How i can set gravity in older versions?
Go to app > res > right-click > New > Android Resource Directory and give Directory name and Resource type as menu. Now, we will create a popup_menu file inside that menu resource directory. Go to app > res > menu > right-click > New > Menu Resource File and create a menu resource file and name it as popup_menu.
Android Popup Menu displays a list of items in a vertical list which presents the view that invoked the menu and is useful to provide an overflow of actions related to specific content.
Change line import android.widget.PopupMenu;
to import android.support.v7.widget.PopupMenu;
you can use rtl for giving gravity to right here is link
RTL was started to support at Android 4.2
or you can create a custom popupmenu and inflate it.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
popup = new PopupMenu(context, arg1,Gravity.CENTER );
//popupWindow.showAtLocation(anchor, Gravity.NO_GRAVITY, xPos, yPos);
}
//Inflating the Popup using xml file
popup.getMenuInflater().inflate(R.menu.listmenu, popup.getMenu());
or you can give anchor and style
showPopup(viewOfWherePopupmenuShows,popmenuNameList.size());
PopupMenu popMenu = new PopupMenu(Activity.this,v)
int size =popmenuNameList.size();
for(int i =0; i <size; i++){
popMenu.getMenu().add(popmenuNameList.get(i)).setIcon(R.drawable.logo);
}
popMenu.show();
protected void showPopupMenu(View v, int size){
//create instance
PopupMenu popup = new PopupMenu(Activity.this,v);
//inflating the popup using xml
popup.getMenuInflater().inflate(R.menu.menu_popup_list),popup.getMenu());
}
The style of menu is determined by the style of context you pass. So all you need to do is to pass your Activity reference as context
style here;
<style name="style" parent="android:Theme.Holo.Light">
<item name="android:popupMenuStyle">...</item>
<item name="android:popupAnimationStyle">...</item>
<item name="android:popupBackground">...</item>
</style>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With