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
How to Make an Android App to Always Run in Background
Adb Root Is Not Working on Emulator (Cannot Run as Root in Production Builds)
How to Get All Android Contacts But Without Those Which Are on Sim
The Signing Fingerprint You Specified Is Already Used by Another Android Oauth2 Client
System.Loadlibrary(...) Couldn't Find Native Library in My Case
How to Add a Line Break in an Android Textview
How to Make a Vertical Seekbar in Android
Intel's Haxm Equivalent for Amd on Windows Os
Creating a Searchview That Looks Like the Material Design Guidelines
Update an Android App (Without Google Play Store Visit)
Android Image View Pinch Zooming
How to Start/ Launch Application at Boot Time Android
Call Method When Home Button Pressed
Noclassdeffounderror When Googleanalyticstracker.Getinstance()
Fool-Proof Way to Handle Fragment on Orientation Change
Android Studio:How to Uninstall APK (Or Execute Adb Command) Automatically Before Run or Debug
How to Open Email Program via Intents (But Only an Email Program)
How to Use Accelerometer to Measure Distance for Android Application Development