I'm trying to create context menu inside fragment. My sequence of actions:
1)Create resource directory and menu file (context_menu.xml)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/download"
android:title="Download"/>
<item android:id="@+id/hello"
android:title="Hello"/>
<item android:id="@+id/bye"
android:title="Bye"/>
2)Setup button inside oncreateView()
val button = view.findViewById<Button>(R.id.download_button)
registerForContextMenu(button)
button.setOnClickListener {
Toast.makeText(requireContext(), "Click", Toast.LENGTH_SHORT).show()
}
Button works. Toast appears.
3)Override method onCkeateContextMenu() inside my fragment
override fun onCreateContextMenu(
menu: ContextMenu,
v: View,
menuInfo: ContextMenu.ContextMenuInfo?
) {
super.onCreateContextMenu(menu, v, menuInfo)
requireActivity().menuInflater.inflate(R.menu.context_menu, menu)
}
Pressing the button does not open the menu. What could be the problem?