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

Questions:

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

How to&Answers:

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

Answer:

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.

Answer:

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

Answer:

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"