The icon in the options menu does not change

138 Views Asked by At

I'm trying to change the icon using Xml and code, but android studio loads default icon.Tell me what is my mistake

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
    inflater.inflate(R.menu.search_menu, menu)
    val searchItem = menu.findItem(R.id.gps_img)
    val searchView = searchItem.actionView as SearchView
    searchView.queryHint = "Current city?"

    searchItem.setIcon(R.drawable.ic_baseline_gps_fixed_24)
    
    searchView.isSubmitButtonEnabled = true
    searchView.setOnQueryTextListener(this)
}

<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">

<item
    android:title="@string/gps"
    android:id="@+id/gps_img"
    app:actionViewClass="androidx.appcompat.widget.SearchView"
    app:showAsAction="ifRoom"
    android:icon="@drawable/ic_baseline_gps_fixed_24"
    />
1

There are 1 best solutions below

0
Aman On

Override public void onPrepareOptionsMenu(@NonNull Menu menu) method, write icon change logic inside it, also call invalidateOptionsMenu() method whenever you want to change menu icon.