4

This is a simple and silly question but I am developing an application in which i am showing a popup menu on click of a button. The code is as follows:

PopupMenu popup = new PopupMenu(mContext, view);
popup.getMenu().add("A");
popup.getMenu().add("B");
popup.getMenu().add("c");
popup.show();

popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
    @Override
    public boolean onMenuItemClick(MenuItem item) {
        // My stuff
    }
}

The issue is : In some devices the popup come at the bottom of button but in some devices the pop up overlaps the button or partially overlap.

How to set the popup, so that it will not overlap the button and the button is completely visible.

RajSharma
  • 1,941
  • 3
  • 21
  • 34
Manoj Fegde
  • 4,786
  • 15
  • 50
  • 95

2 Answers2

-1

You could use the showAtLocation() method of the PopupWindow class to locate the PopupWindow on a specific position on the screen like this for example:

PopupWindow popup = new PopupWindow(mContext, view);
popup.getMenu().add("A");
popup.getMenu().add("B");
popup.getMenu().add("c");
//takes the parent view, gravity and location offsets as paremetrs
popup.showAtLocation(view, Gravity.CENTER, 0, coordinate);

Here you could find more answers: How to show PopupWindow at special location?

Community
  • 1
  • 1
Gabriella Angelova
  • 2,985
  • 1
  • 17
  • 30
  • Can you please tell me what coordinate is? I am giving int value at there but it is giving error. – Manoj Fegde Jul 01 '15 at 08:42
  • Both last two parameters are integers - width and height, try for example with width = 150 and height = 300 – Gabriella Angelova Jul 01 '15 at 08:47
  • I am using PopupMenu not PopupWindow. So it is giving me error as : The method showAtLocation(View, int, int, int) is undefined for the type PopupMenu – Manoj Fegde Jul 01 '15 at 08:48
  • Oh, sorry, my fault... Then try with this for example `PopupMenu popup = new PopupMenu(this, v,Gravity.CENTER);` where `this` is the context, `v` is the parent view and the third parameter is the gravity of your popupMenu – Gabriella Angelova Jul 01 '15 at 08:51
  • PopupMenu popup = new PopupMenu(this, v,Gravity.CENTER); requires API level 19, but mine app is on 14, and it is requirement of app. – Manoj Fegde Jul 01 '15 at 08:54
-1

use popup.showAtLocation(view, Gravity.CENTER, 0, coord); to position this according to your need.

Yoi can refer to this example also. here

Community
  • 1
  • 1
RajSharma
  • 1,941
  • 3
  • 21
  • 34