I'm trying to change the icon for my own in SaerchView
, but for some reason I do not understand, the icon does not change.
Here is my toolbar_menu.xml
:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/toolbar_action_search" android:icon="@android:drawable/ic_menu_search" android:title="@android:string/search_go" app:actionViewClass="android.support.v7.widget.SearchView" app:showAsAction="ifRoom|collapseActionView" /> <item android:id="@+id/toolbar_action_sort" android:icon="@drawable/ic_sort" android:title="@string/action_sort" app:actionViewClass="android.widget.Spinner" app:showAsAction="always"/> </menu>
This is how I try to change the icon:
@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.home_toolbar_menu, menu); toolbarSearchView = (SearchView) menu.findItem(R.id.toolbar_action_search).getActionView(); ImageView searchButtonIcon = (ImageView) toolbarSearchView.findViewById(android.support.v7.appcompat.R.id.search_button); searchButtonIcon.setImageResource(R.drawable.ic_search); return true; }
I understand that I can simply change the XML android:icon
. But in the main project I need to change the icon programmatically and the above is just an example to describe the problem.
For what reason can the icon change be ignored?
menu.findItem().setIcon()
- andreich