How to change popup menu position on Android overflow button?

I just like to implement the same thing as the pop-up menu in the Gmail application, tied to the overflow button in the upper right corner. for this, I used the same code as the Google tutorial for the Android Android popup menu , but for me to show the popup menu at the top of the action bar is not under that. If you notice the Gmail Overflow pop-up menu, you see that the pop-up menu is on the edge of the action bar.

This is the XML I used for the 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 subsequently in my work:

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;
        }
    });
}
+6
5

:

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;
}

XML res/layout 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>
+2

, :

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

PopupMenu , :

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>

:

ContextThemeWrapper contextThemeWrapper = new ContextThemeWrapper(getContext(), R.style.PopupMenuOverlapAnchor);
PopupMenu popupMenu = new PopupMenu(contextThemeWrapper, this);

: PopupMenu. (overflowButton), :

    PopupMenu popupMenu = new PopupMenu(context, overflowMenuButton);
    popupMenu.inflate(R.menu.my_menu);

    // Fix vertical offset
    overflowMenuButton.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            popupMenu.show();
            if (popupMenu.getDragToOpenListener() instanceof ListPopupWindow.ForwardingListener)
            {
                ListPopupWindow.ForwardingListener listener = (ListPopupWindow.ForwardingListener) popupMenu.getDragToOpenListener();
                listener.getPopup().setVerticalOffset(-overflowMenuButton.getHeight());
                listener.getPopup().show();
            }
        }
    });

a >

+40

PopupMenu popup = new PopupMenu(this, v, Gravity.RIGHT);
+5
popupwindow.showAsDropDown(view,x,y);

x y .

0
source

After trying out every approach that I found, I would think that putting a binding view can still be simpler and easier, especially when you use a more flexible layout like ConstraintLayout.

Just place the invisible view where you want to snap the popup menu:

<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 the binding view instead:

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

Boom, it's done.

0
source

Source: https://habr.com/ru/post/1539561/


All Articles