Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the default icon on the SearchView, to be use in the action bar on Android?

I'm having a bit of trouble customizing the search icon in the SearchView. On my point of view, the icon can be changed in the Item attributes, right? Just check the code bellow..

Can someone tell me what I'm doing wrong?

This is the menu I'm using, with my custom search icon icn_lupa. But when I run the app, I always get the default search icon...

<menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menu_search"       android:title="@string/menu_search"       android:icon="@drawable/icn_lupa"       android:showAsAction="always"       android:actionViewClass="android.widget.SearchView" /> </menu> 

Thanks in advance.

like image 939
Valdemar Avatar asked May 04 '12 08:05

Valdemar


People also ask

How to implement SearchView in ToolBar in Android?

Add the Search View to the App Bar To add a SearchView widget to the app bar, create a file named res/menu/options_menu. xml in your project and add the following code to the file. This code defines how to create the search item, such as the icon to use and the title of the item.

What is action bar icon?

The ActionBar, now known as the App Bar, is a consistent navigation element that is standard throughout modern Android applications. The ActionBar can consist of: An application icon. An "upward" navigation to logical parent. An application or activity-specific title.

How do I change the Search icon on my Android?

There's a way to do this. The trick is to recover the ImageView using its identifier and setting a new image with setImageResource() . This solution is inspired on Changing the background drawable of the searchview widget. To clarify, this is to change the default icon when the search view is "expanded."


1 Answers

I've found another way to change the search icon which goes in the same line as Diego Pino's answer but straight in onPrepareOptionsMenu.

In your menu.xml (same as before)

<menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/action_search"       android:icon="@drawable/ic_action_fav"       android:title="@string/action_websearch"       android:showAsAction="always|never"       android:actionViewClass="android.widget.SearchView" /> </menu> 

In your activity:

@Override public boolean onPrepareOptionsMenu(Menu menu) {     MenuItem searchViewMenuItem = menu.findItem(R.id.action_search);         mSearchView = (SearchView) searchViewMenuItem.getActionView();     int searchImgId = getResources().getIdentifier("android:id/search_button", null, null);     ImageView v = (ImageView) mSearchView.findViewById(searchImgId);     v.setImageResource(R.drawable.your_new_icon);      mSearchView.setOnQueryTextListener(this);     return super.onPrepareOptionsMenu(menu); } 

I followed the example for changing the edittext in this example.

You should be able to do this for all icons/backgrounds in your SearchView, to find the right ID you can check here.

Hope this helps someone else as well!

UPDATE November 2017:

Since this answer android has been updated with the possibility of changing the search icon through the XML.

If you target anything below android v21 you can use:

<android.support.v7.widget.SearchView     android:layout_width="wrap_content"     android:layout_height="match_parent"     app:searchIcon="@drawable/ic_search_white_24dp"     app:closeIcon="@drawable/ic_clear_white_24dp" /> 

Or v21 and later:

<SearchView     android:layout_width="wrap_content"     android:layout_height="match_parent"     android:searchIcon="@drawable/ic_search_white_24dp"     android:closeIcon="@drawable/ic_clear_white_24dp" /> 

And there are even more options:

closeIcon commitIcon goIcon searchHintIcon searchIcon voiceIcon 
like image 186
just_user Avatar answered Sep 23 '22 06:09

just_user