T
T
Tsuzukeru2021-02-19 19:48:58
Android
Tsuzukeru, 2021-02-19 19:48:58

Why doesn't the context menu open in the fragment?

I'm trying to create a context menu inside a fragment. I perform the following sequence of actions:

1) I create a resource directory and a 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"/>
</menu>

2) Inside onCreateView() I set up button:
val button = view.findViewById<Button>(R.id.download_button)
        registerForContextMenu(button)
        button.setOnClickListener {
            Toast.makeText(requireContext(), "Click", Toast.LENGTH_SHORT).show()
        }

When you press the button, the toast is triggered.

3) Inside the fragment, I override the onCkeateContextMenu() method
override fun onCreateContextMenu(
        menu: ContextMenu,
        v: View,
        menuInfo: ContextMenu.ContextMenuInfo?
    ) {
        super.onCreateContextMenu(menu, v, menuInfo)
        requireActivity().menuInflater.inflate(R.menu.context_menu, menu)
    }


When you click on the menu button, it does not open. What could be the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2021-02-20
@zagayevskiy

it will work with a long press, judging by the documentation

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question