9

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

Saiteja Prasadam
  • 499
  • 3
  • 17

5 Answers5

13

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

Saiteja Prasadam
  • 499
  • 3
  • 17
8

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.

paulina_glab
  • 2,467
  • 2
  • 16
  • 25
Mohit Dixit
  • 176
  • 1
  • 8
1

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

Misagh
  • 3,403
  • 1
  • 20
  • 17
0

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"

koksalb
  • 430
  • 8
  • 14
  • How can i achieve it, can you provide me some steps – Saiteja Prasadam Aug 09 '16 at 10:37
  • well I do not have your full layout so I don't know how your LinearLayouts and Floating items etc is located. But I can explain the main idea and you will get it after there. so the problem is that popup disobeys your edges so you have to replace it somewhere it will be okay on the look from outside right? you leave the popup there but you inflate it inside another View which is fake and located/floating) lets say a bit on the left side. so when the popupmenu inflates inside of it it will look perfectly in the right place. You cheat the outlooking of the design. good luck – koksalb Aug 09 '16 at 10:41
  • again i am saying it is definetally not the best solution and it sounds like you give up on your coding talents and try to cheat. but isnt it all about? to find an easy way to do something right. following the thread if someone suggests a code related solution so I can also learn and fix my old app. – koksalb Aug 09 '16 at 10:43
  • It may not work perfectly on large devices or different sdk (Correct me if am wrong) – Saiteja Prasadam Aug 09 '16 at 10:53
  • not the best solution i said already. But with this idea the same, it is all about your designing. you can place a layout that looks like a popup menu and place choices inside of it with fixed size and have a scroolview inside of it also so it would be useful even with 100 choices. – koksalb Aug 09 '16 at 11:08
  • That won't be an optimal solution, Thanks for the suggestion... Maybe i will implement it with the dialog box. – Saiteja Prasadam Aug 09 '16 at 12:07
0

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

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

Harshal Pudale
  • 178
  • 1
  • 2
  • 7