How to Change the Default Icon on the Searchview, to Be Use in the Action Bar on Android

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

It looks like the actionViewClass overides the icon and it doesn't look like you can change it from this class.

You got two solutions:

  • Live with it and I think it's the best option in terms of user experience and platform conformity.
  • Define your own actionViewClass

How to change SearchView default icon?

Unfortunately, there is no easy way to change the SearchView icon to a custom drawable, since the theme attribute searchViewSearchIcon is not public. See this answer for details.

However, I think that your problem is caused by inheriting from the wrong theme. Please use android:Theme.Holo.Light.DarkActionBar as the base for your theme. Then the default icons on the action bar should have a light color.

<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
...
</style>

Android: SearchView component change icon (no ActionBar)

It´s possible to use AppCompat.

But I used small hack:

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);

from this How to change the default icon on the SearchView, to be use in the action bar on Android? and it looks ok.

Change the icons of Search in Actionbar

This question ma be related with your problem. Please check it.

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

Change default search icon position in search view

So finally I found solution.
note that it is not a good solution. but it's works fine for me.

in menu_main.xml:

<item
android:id="@+id/action_search"
android:title="@android:string/search_go"
android:icon="@drawable/search"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView"
android:visible="false"/>

in the menu file i set the visibility of search icon to the false. so now when user clicks on my search icon in the toolbar i set the visibility to true.

imgCatSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mSearchView.setBackgroundColor(Color.WHITE);
Animation anim = AnimationUtils.loadAnimation(G.context,R.anim.m_anim);
searchMenuItem.setVisible(true);
mSearchView.setIconified(false);
mSearchView.startAnimation(anim);
}
});

So when user clicks on search button in action bar the search view visibility turned to true and opened.

finally this code is for when user clicks on close button in searchView :

mSearchView.setOnCloseListener(new SearchView.OnCloseListener() {
@Override
public boolean onClose() {
searchMenuItem.setVisible(false);
return false;
}
});


Related Topics



Leave a reply



Submit