Home » Android » android – Popup menu goes out of the screen

android – Popup menu goes out of the screen

Posted by: admin June 15, 2020 Leave a comment


Popup menu on inflate goes out of the screen for this activity, works fine for other

enter image description here

Extending custom AppCompatActivity


<style name="AppTheme.ActionBar.TransparentContextTranscition" parent="AppTheme.NoActionBar">
    <item name="android:windowActivityTransitions">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowContentTransitions">true</item>
    <item name="android:windowLightStatusBar">true</item>

    <item name="android:itemBackground">@android:color/white</item>
    <item name="android:textColor">@android:color/black</item>
    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
    <item name="android:popupMenuStyle">@style/PopupMenu</item>

<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
    <item name="color">@android:color/white</item>

<style name="PopupMenu" parent="@android:style/Widget.PopupMenu">
    <item name="android:popupBackground">@android:color/white</item>

Java code

holder.contextMenuView.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            final PopupMenu popup = new PopupMenu(v.getContext(), v);

Toolbar inflate menu works fine

enter image description here

How to&Answers:

Looks like I was using


my bad. FLAG_LAYOUT_NO_LIMITS, removes limits for the context menu. (That’s the reason, the context menu going out of the screen), Hope that helps for someone


Use this in your code:

final PopupMenu popup = new PopupMenu(v.getContext(), v, Gravity.RIGHT);

if it doesn’t work then change the anchor view v. because it can set the position of the popup menu as per your requirement.


A simple yet-customizable Android drop-down menu. It supports Text with/without Icons, Separators, and even fully customized views



Unfortunately popup menu does not follow the size of the view it is inside and goes off scope. I had the same problem before. And I know it is not the best solution but here it goes:

Create a fake view which is transparent to use as anchor to block the edge from exceeding.

However since it will be floating you cannot place it on the top or bottom side so careful on the height exceeding while trying this.

And by the way do NOT forget to set android:anyDensity="True/False"