Search code examples
androidkotlinfragmentandroid-toolbar

Remove overflow menu 3 dots in fragment


I have a fragment that needs to have a custom menu. Below is the code which is added to my fragment.

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setHasOptionsMenu(true)
    }

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

My actionbar_menu is as follows:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/search"
        android:icon="@drawable/search"
        android:title="Search"
        app:showAsAction="always"
        tools:ignore="AlwaysShowAction" />
</menu>

I only want this search icon to be present in the toolbar. What happens is its adding overflow menu too. How can I remove the overflow menu?

UPDATE: I am getting this issue only in mobiles. In tablet emulators its not showing up.


Solution

  • It worked when I called the same in different way.

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        setHasOptionsMenu(true)
    }
    
    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
        menu.clear()
        inflater.inflate(R.menu.actionbar_menu, menu)
    }