Skip to content Skip to sidebar Skip to footer

How Change Position Of Popup Menu On Android Overflow Button?

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 andr

Solution 1:

To overlap only, use this approach:

PopupMenupopupMenu=newPopupMenu(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

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

Code:

ContextThemeWrappercontextThemeWrapper=newContextThemeWrapper(getContext(), R.style.PopupMenuOverlapAnchor);
PopupMenupopupMenu=newPopupMenu(contextThemeWrapper, this);

Solution 2:

Applying gravity helped in my case

PopupMenupopup=newPopupMenu(this, v, Gravity.RIGHT);

Solution 3:

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 eventpublic PopupWindow popupDisplay() { // disply designing your popoup windowfinalPopupWindowpopupWindow=newPopupWindow(this); // inflet your layout or diynamic add view

    View view; 

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

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

    Buttonitem= (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

<LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="horizontal" ><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Window test" /></LinearLayout>

Solution 4:

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.

Solution 5:

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.

Post a Comment for "How Change Position Of Popup Menu On Android Overflow Button?"