Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 916
Saiteja Prasadam Avatar asked Jul 31 '16 03:07

Saiteja Prasadam


People also ask

How do I show the pop up menu on Android?

Go to app > res > right-click > New > Android Resource Directory and give Directory name and Resource type as menu. Now, we will create a popup_menu file inside that menu resource directory. Go to app > res > menu > right-click > New > Menu Resource File and create a menu resource file and name it as popup_menu.

What displays a pop up menu?

Pop-up menus appear over the application in a vertical orientation while the user is clicking an item, and then they disappear from the screen. You can programmatically invoke a pop-up menu, or you can arrange for a right-mouse click to automatically invoke a pop-up menu tied to a window or a control.


2 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

like image 117
Saiteja Prasadam Avatar answered Nov 15 '22 17:11

Saiteja Prasadam


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.

like image 23
Mohit Dixit Avatar answered Nov 15 '22 18:11

Mohit Dixit