1

Here is my code I want to update menu item icon based on data I requesting in server that is why I need to have access to menu item in onViewCreated as I think solution which I am doing now is not right as observing data in onPrepareOptionsMenu?

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


override fun onPrepareOptionsMenu(menu: Menu) {
    super.onPrepareOptionsMenu(menu)
    viewModel.favorite
        .observe(viewLifecycleOwner, Observer {
          if (it.data != null) {
            menu.findItem(R.id.action_favorite)?.icon = true
          }
        })
  }
Artur A
  • 257
  • 3
  • 20
  • 1
    Possible duplicate of [How to change MenuItem icon in ActionBar programmatically](https://stackoverflow.com/questions/19882443/how-to-change-menuitem-icon-in-actionbar-programmatically) – gromyk Jun 01 '19 at 00:14
  • check this answer : https://stackoverflow.com/questions/33042431/setting-menuitem-icon-from-url-android – ismail alaoui Jun 01 '19 at 02:04

0 Answers0