15

I just like to implement somethings same as popup menu in the Gmail app, anchored to the overflow button at the top-right. for that I used the same code as google tutorial for android Android popup menu, but for me show pop menu on top of edge of actionbar not under that. If you notice on pop menu of gmail overflow you saw that popmenu take place at edge of actionbar.

This is the xml that I used for popup menu:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/item1"
        android:title="lablab"/>
    <item
        android:id="@+id/item2"
        android:title="lablab"/>

</menu>

and at the follow is in my activity:

public void showFontSetting(View view) {
    PopupMenu popup = new PopupMenu(this, view);
    MenuInflater inflater = popup.getMenuInflater();
    inflater.inflate(R.menu.menu, popup.getMenu());
    popup.show();

    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
        // TODO Auto-generated method stub

            switch (item.getItemId()) {
                case R.id.item1:
                    Toast.makeText(Index.this,
                        "You Clicked : " + item.getTitle(),
                    Toast.LENGTH_SHORT).show();
                    break;
                case R.id.item2:
                    break;
            }
            return true;
        }
    });
}
TechSpellBound
  • 2,505
  • 6
  • 25
  • 36
user3266062
  • 522
  • 1
  • 7
  • 21

5 Answers5

58

To overlap only, use this approach:

PopupMenu popupMenu = new PopupMenu(getContext(), this, Gravity.NO_GRAVITY, R.attr.actionOverflowMenuStyle, 0);

To get a PopupMenu with a bright background and a detailed control over the offsets use this approach:

styles.xml

<style name="PopupMenuOverlapAnchor" parent="@style/Theme.AppCompat.Light">
   <item name="android:overlapAnchor">true</item>
   <item name="android:dropDownVerticalOffset">0dp</item>
   <item name="android:dropDownHorizontalOffset">0dp</item>
</style>

Code:

ContextThemeWrapper contextThemeWrapper = new ContextThemeWrapper(getContext(), R.style.PopupMenuOverlapAnchor);
PopupMenu popupMenu = new PopupMenu(contextThemeWrapper, this);
user1185087
  • 4,468
  • 1
  • 30
  • 38
  • Any idea why `popupMenu.getDragToOpenListener()` is an `android.widget.PopupMenu` for me? – danijar Dec 20 '15 at 18:51
  • I think you analyzed the debug output and detect sth like this: mDragListener = {android.support.v7.widget.PopupMenu$1@7128}. Have a detailed look on the output: The last two signs before the @ are a 1 and a $ which means that the object is an inner anonymous class instance. A real instance of PopupMenu looks like that: this = {android.support.v7.widget.PopupMenu@7123} without $1. The source code of PopupMenu reveals also this fact: mDragListener = new ListPopupWindow.ForwardingListener(mAnchor) {...}; – user1185087 Dec 29 '15 at 11:40
  • This does not work. Maybe is worked in the past, but not any longer. Class ListPopupWindow does not define/declare a ForwardingListener. ForwardingListener is located in android.support.v7.widget and its listener.getPopup() does not have a method setVerticalOffset(). – M.Paunov Nov 28 '16 at 11:41
  • `android:dropDownVerticalOffset` and `android:dropDownHorizontalOffset` are not working for me, but the bright background works. – SeaJelly Jul 21 '17 at 19:03
  • @user1185087: please share actionOverflowMenuStyle from R.attr.actionOverflowMenuStyle – Jayesh Dec 29 '17 at 06:32
  • It's truly replicates the overflow popup menu if the `Gravity` is set to `Gravity.END`. – Rajarshi Jan 13 '19 at 13:06
13

Applying gravity helped in my case

PopupMenu popup = new PopupMenu(this, v, Gravity.RIGHT);
Neeraj
  • 499
  • 8
  • 16
  • 1
    Sorry, forgot to mention, this has been added in Api 19 https://developer.android.com/reference/android/widget/PopupMenu.html#PopupMenu(android.content.Context,%20android.view.View,%20int) – Neeraj Jul 20 '16 at 12:50
  • 2
    but it's in the support lib for all versions. – Frank Oct 24 '16 at 09:58
6

Add the following piece of code to your activity:

PopupWindow popupwindow_obj; // create object

popupwindow_obj=popupDisplay();  // initialize in onCreate()

popupwindow_obj.showAsDropDown(clickbtn,-40, 18); // where u want show on view click event

public PopupWindow popupDisplay() { // disply designing your popoup window
    final PopupWindow popupWindow = new PopupWindow(this); // inflet your layout or diynamic add view

    View view; 

    LayoutInflater inflater = (LayoutInflater)   getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    view = inflater.inflate(R.layout.mylayout, null);

    Button item = (LinearLayout) view.findViewById(R.id.button1);

    popupWindow.setFocusable(true);
    popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
    popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
    popupWindow.setContentView(view);

    return popupWindow;
}

Create an XML in res/layout directory and name it mylayout.xml

<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Window test" />
</LinearLayout>
TechSpellBound
  • 2,505
  • 6
  • 25
  • 36
SuN
  • 1,723
  • 1
  • 13
  • 14
  • I am new on android and I searched but I can't find any solution for Inflate my layout. – user3266062 May 07 '14 at 12:35
  • View view; LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = inflater.inflate(R.layout.mylayout, null); RelativeLayout item = (RelativeLayout) view.findViewById(R.id.item); – SuN May 07 '14 at 12:38
  • What should I put Instead of mylayout on inflate. I got lots of error. – user3266062 May 07 '14 at 13:26
  • :) you put your own layout. xml file – SuN May 07 '14 at 13:28
  • Did you see my edit for adding code? respect to it I added your code and put view = inflater.inflate(R.menu.menu, null); RelativeLayout item = (RelativeLayout) view.findViewById(R.id.item1); and call it to my showFontSetting method of my code. But I recived lots of exception. – user3266062 May 07 '14 at 13:30
3

After trying out each approach that I have found, I would think putting an anchoring view might still be an easier and simpler way, especially when you are using a more flexible layout, e.g. ConstraintLayout.

Just put an invisible view to where you want the popup menu to anchor:

<View
    android:id="@+id/anchor_menu"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginTop="10dp"
    app:layout_constraintStart_toStartOf="@+id/button_menu"
    app:layout_constraintTop_toBottomOf="@+id/button_menu"
    />

Then use it as the anchoring view instead:

    mPopupMenu = new PopupMenu(getActivity(), mPopupMenuAnchor);

Boom, it is done.

Stephen Liu
  • 141
  • 4
0

Maybe you might consider using PopupWindow instead.

popupwindow.showAsDropDown(view,x,y);

Here x and y is position of popup window relative to your view.

soshial
  • 5,906
  • 6
  • 32
  • 40
Sanket Kachhela
  • 10,861
  • 8
  • 50
  • 75