Android PopupMenu setGravity (API <19)

In Android API 19, the PopupMenu contourner was added to define gravity.

public PopupMenu (context context, bind view, int gravity)

How do I set gravity in older versions?

+3


source share


2 answers


Change the line import android.widget.PopupMenu;

to importandroid.support.v7.widget.PopupMenu;



+6


source


you can use rtl to provide gravity to the right here link

RTL was launched for support on Android 4.2

or you can create your own popup menu 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 the binding 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 the menu is determined by the style of the context you are going through. So all you have to do is pass the link to your activity as context

the style is 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>

      

0


source







All Articles