Home > Net >  Android: How to change the background of the menu item in the toolbar
Android: How to change the background of the menu item in the toolbar

Time:11-28

You need to change the background of the ~showAsAction = always element as in the example.

enter image description here

I tried to make custom xml through a layer-list with a background and an icon, but as a result, the background was only inside the icon, and not around the entire perimeter of the item in the menu. Also tried android(app):background, android(app):itemBackground, but it`s dont work

My menu xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:id="@ id/action_search"
    android:layout_width="wrap_content"
    android:icon="@drawable/ic_search"
    android:title="Search"
    app:actionViewClass="androidx.appcompat.widget.SearchView"
    app:showAsAction="collapseActionView|ifRoom" />
<item
    android:id="@ id/action_filter"
    android:layout_width="wrap_content"
    android:icon="@drawable/ic_slider"
    app:iconTint="@color/green"
    android:iconTint="@color/green"
    android:title="filters"
    app:showAsAction="collapseActionView|ifRoom"
    />

My adding menu in fragment

val menuHost: MenuHost = binding.searchAnimeToolbar
    menuHost.addMenuProvider(object : MenuProvider {
        override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
            menuInflater.inflate(R.menu.menu, menu)
            val menuFilterButton = menu.findItem(R.id.action_filter)


        }

        override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
            if(menuItem.itemId == R.id.action_filter) {
                Toast.makeText(context, "coming soon", Toast.LENGTH_LONG).show()
            }

            return true
        }
    }, viewLifecycleOwner)

Update: This method with the addition of a separate drawable is not suitable, because it is necessary to set the background for the menu item, and not for the icon.

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <layer-list>
        <item>
            <shape android:shape="rectangle">
                <solid android:color="@color/opacity_green"/>
                <corners android:radius="2dp"/>
            </shape>
        </item>
        <item android:drawable="@drawable/ic_slider">
        </item>
    </layer-list>
</item>
</selector>

CodePudding user response:

First create a drawable for your menu item.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/your_drawable" />
    <item android:state_selected="true" android:drawable="@drawable/your_drawable" />
    <item android:state_focused="true" android:drawable="@drawable/your_drawable" />
    <item android:drawable="@drawable/your_drawable" />
</selector>

Then put it as icon for Menu Item.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    <item android:id="@ id/itemTodo"
        android:icon="@drawable/item_selector"
        android:title="TODO"
        app:showAsAction="always"/>
</menu>

Hope it helps you. Happy coding!

  • Related