'Popup menu goes out of the screen

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

enter image description here

Extending custom AppCompatActivity

Styles:

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

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

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

Java code

holder.contextMenuView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final PopupMenu popup = new PopupMenu(v.getContext(), v);
            popup.inflate(R.menu.song_item_menu_album_inner_layout);
        }
});

Toolbar inflate menu works fine

enter image description here



Solution 1:[1]

Looks like I was using

getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

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

Solution 2:[2]

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.

Solution 3:[3]

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

https://github.com/shehabic/Droppy

Solution 4:[4]

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"

Solution 5:[5]

You can allow the popup menu to overlap with the anchor view.

More here: https://stackoverflow.com/a/29702608

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Saiteja Prasadam
Solution 2 paulina_glab
Solution 3 Misagh
Solution 4
Solution 5 Harshal Pudale