Popup menu on inflate goes out of the screen for this activity, works fine for other
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
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With