4

I got lost trying to control onclick for options menu in fragments. There are so many solutions that I don't even know where to start.

I saw some places where people tell us to use NavController, others tell us to use onOptionsItemSelected both in the activity and fragment and override what you need. On top of that we have the androidx.appcompat.widget.Toolbar that doesn't even need to be inflated, just use app:menu= in the XML file and boom all there but how do I listen to the clicks ?

<androidx.appcompat.widget.Toolbar
            app:menu="@menu/menu_fragment_main">

I had it working in the activity but then had to use fragments instead and got into this mess, I just want the menu to work like it was working in the activity.

2
  • Did you call setHasOptionsMenu(true) in onCreate? Sep 21, 2019 at 8:15
  • I did, didn't work :(. Sep 21, 2019 at 15:12

3 Answers 3

5

You need to call setHasOptionsMenu in fragment onCreate() method:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

Here is how you inflate menu and respond to click :

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu, menu);
    super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
  
    case R.id.refresh:
        //Respond to click
        break;
    }
    return true;
}

And here is your menu.xml :

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/refresh" android:title="Refresh" android:icon="@drawable/ic_action_refresh" />
</menu>
1
  • 2
    I tried this, it did not work. Also its java. Also it inflates, with app:menu="@menu/menu_fragment_main" the menu is inflated too. I saw someplaces that for fragment I need to call oncreateoptionsmenu in the activity too. Sep 21, 2019 at 15:12
1
val toolbar = fragmentInflater.findViewById<androidx.appcompat.widget.Toolbar>(R.id.toolbarMainActivity)
        (activity as AppCompatActivity).setSupportActionBar(toolbar)

This solved the problem, kind of tired now but I well come comments on why this happened latter.

I think it was some mix of , toolbar in xml, androidx, wrong imports for toolbar non android x and so on.

2
  • 1
    Have the same problem, but don't understand this your answer/solution. Actually I am using NavController.
    – Emaborsa
    Feb 13, 2020 at 15:53
  • Ok its been a while let me try to remember. I think the solution might be to use androidx stuff, ill look at my app and see how it is going setSupportActionBar(toolbarPriority) toolbarPriority.title = "Priority" and in the xml <com.google.android.material.appbar.AppBarLayout Does that help ? Mar 23, 2020 at 22:31
0

In sample case, a overflow_menu created and that is connected to AboutMe activity/fragment. It uses navigation direction to go to about me page -

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:android="http://schemas.android.com/apk/res/android">
   <item
     android:id="@+id/about"
     android:title="@string/about" />
</menu> 

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.main_activity)
    setHasOptionsMenu(true)
}

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
    super.onCreateOptionsMenu(menu, inflater)
    inflater.inflate(R.menu.overflow_menu, menu)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    when (item.itemId) {
        R.id.about -> navigateAbout(item)
    }
    return super.onOptionsItemSelected(item)
}

private fun navigateAbout(item: MenuItem) {
    view!!.findNavController()
        .navigate(TitleFragmentDirections.actionTitleFragmentToAboutFragment())
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.