1

How can we change the application icon that appears on left when the SearchView widget is expanded?

Here is my onCreateOptionsMenu() code:

 public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    final TextView tv = (TextView)findViewById(R.id.textView);
    SearchView searchView = (SearchView)menu.findItem(R.id.menu_search).getActionView();
    searchView.setQueryHint("Search");

    return super.onCreateOptionsMenu(menu);
 }

And I have the following in xml for searchView widget:

<item android:id="@+id/menu_search" android:title="search"
android:showAsAction="ifRoom|collapseActionView"
android:actionViewClass="android.widget.SearchView" />

I also looked through the search_view.xml ( https://github.com/android/platform_frameworks_base/blob/master/core/res/res/layout/search_view.xml#L75 ),that is inflated by default, to check if I can get the element from there and then change the image. But I could not find any element in this xml that is responsible for collapsing the search view widget.

andro
  • 977
  • 1
  • 10
  • 21

1 Answers1

0

Try this :

SearchView searchView = new SearchView(getContext());

OptionCommand searchCommand = new OptionCommand();
searchCommand.setActionView(searchView);
searchCommand.setIcon(getResources().getDrawable(android.R.drawable.ic_menu_search));
searchCommand.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
searchView.setBackgroundColor(Color.WHITE);
lokoko
  • 5,785
  • 5
  • 35
  • 68