I am using appcompat library with a toolbar and I have implemented a search view in my app. But i want to remove the search view icon when its expanded.
here is my menu xml file:
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_search"
android:title="#string/action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView"/>
and this is how I inflate the menu:
getMenuInflater().inflate(R.menu.main, menu);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
searchView.setQueryHint(getString(R.string.search_hint));
return super.onCreateOptionsMenu(menu);
this is what i want to remove:
This helped me to remove Search icon
searchView.setIconified(false);
If you want searchView always expanded use just this:
searchView.onActionViewExpanded();
Try this:
In your app theme, add the following line:
<item name="searchViewStyle">#style/MySearchViewStyle</item>
Add define the style at below:
<style name="MySearchViewStyle" parent="Widget.AppCompat.SearchView">
<item name="searchHintIcon">#null</item>
</style>
try {
Field mDrawable = SearchView.class.getDeclaredField("mSearchHintIcon");
mDrawable.setAccessible(true);
Drawable drawable = (Drawable) mDrawable.get(searchView);
drawable.setBounds(0, 0, 0, 0);
} catch (Exception e) {
e.printStackTrace();
}
setBounds does the magic
The simplest way is Here... In case of AppCompat
app:iconifiedByDefault="false"
app:searchIcon="#null"
OR in other case
android:iconifiedByDefault="false"
android:searchIcon="#null"
<item android:id="#+id/menu_search"
android:icon="#drawable/action_search"
app:actionLayout="#layout/xx_layout"
app:showAsAction="always"/>
and in xx_layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.SearchView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/search_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:searchHintIcon="#null"
android:iconifiedByDefault="true" />
In your onCreateOptionsMenu
int searchPlateId = searchView.getContext().getResources()
.getIdentifier("android:id/search_src_text", null, null);
EditText searchPlate = (EditText) searchView
.findViewById(searchPlateId);
searchPlate.setHint("");
If you look at the android source code for the layout of SearchView , you will see that the search icon has a id search_mag_icon. So in onCreateOptionsMenu,
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_search,menu);
MenuItem searchViewItem = menu.findItem(R.id.search);
SearchView searchView = (SearchView)searchViewItem.getActionView();
searchView.findViewById(R.id.search_mag_icon).setVisibility(View.GONE);
}
Just find the view by id and set its visibility to gone.
For androidx and new version of SearchView:
If you add SearchView from code, it's effective for that.
Add new style in styles.xm
<style name="SearchViewMy" parent="Widget.AppCompat.Light.SearchView">
<item name="searchHintIcon">#null</item>
</style>
and applied that style in your app theme:
<style name="Theme.App" parent="Theme.AppCompat.Light.NoActionBar.FullScreen">
<item name="searchViewStyle">#style/SearchViewMy</item>
</style>
Or if you add SearchView from xml, then add this line in your layout:
app:searchHintIcon="#null"
Look into SumeetP's answer in this question. Not sure if that works because I never used it. He gets Search icon as an ImageView. So I guess you can just set image to android.R.color.transparent so that it won't be visible.
If you have a customized search view just add this in your .xml layout
app:searchIcon="#null"
app:iconifiedByDefault="false"
That's an old question, but this helped me a lot in my case: If you are trying to hide inside a menu, none of the questions posted here worked perfectly for me (Greetings for AK5T that almost solved my problem). What solved the problem was:
Instead of putting
app:actionViewClass="android.widget.SearchView"
inside the item in menu, you can change for:
app:actionLayout="#layout/search_layout"
And, inside this layout, you can add this piece of code:
<?xml version="1.0" encoding="utf-8"?>
<android.widget.SearchView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/search_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:searchHintIcon="#drawable/nothing_drawed"/>
And, finally, inside the drawable you just create it with nothing inside, example:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
</selector>
Only this method worked for me. When I tried to hide the searchHintIcon, setting to #null or anything else, it remained the same thing as if I had written no code.
Related
I have a menu item that is showing up on android 4.x but not on 2.x. Here is my menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/menu_filter"
android:title="Filter"
app:showAsAction="always"/>
</menu>
This is my actionbar style
<style name="style1_actionBar" parent="#style/Widget.AppCompat.Light.ActionBar">
<item name="android:background">#color/blue_dark</item>
<item name="android:textColor">#color/white</item>
<item name="actionMenuTextAppearance">#color/white</item>
<item name="background">#color/blue_dark</item>
</style>
Any ideas?
Edit: removed double quote typo
Could it be the fact that I am showing only text, no icons? I'm kind of stuck here.
Whew, thanks for your help guys but I managed to figure it out. It wasn't a problem with the xml, it was a problem with the onCreateOptionsMenu function.
I was using this
new MenuInflater(getApplication()).inflate(R.menu.activity_wentry_editor, menu);
instead of this
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_wentry_editor, menu);
Not entirely sure why this works but it does.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
**xmlns:yourapp="http://schemas.android.com/apk/res-auto"** >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
**yourapp**:showAsAction="ifRoom" />
</menu>
Please refer to the documentation. http://developer.android.com/guide/topics/ui/actionbar.html
Using XML attributes from the support library
Notice that the showAsAction attribute above uses a custom namespace defined in the tag. This is necessary when using any XML attributes defined by the support library, because these attributes do not exist in the Android framework on older devices. So you must use your own namespace as a prefix for all attributes defined by the support library.
In my case I had to add a few lines to onCreateOptionsMenu.
Android Studio didn't let me use android:showAsAction="ifRoom" while using appCompat.
app:showAsAction="ifRoom" wasn't working and I removed it without problems.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
for (int i = 0; i < menu.size(); i++) {
menu.getItem(i).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
return super.onCreateOptionsMenu(menu);
}
If you want your app to support action bar below 3.0 you need to use app compact v7 from the support library.
Also check the link
Using the menu in an activity that extends the AppCompact it is necessary import the app context in the XML and use it:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- "Mark Favorite", should appear as action button if possible -->
<item
android:id="#+id/action_favorite"
android:icon="#drawable/ic_favorite_black_48dp"
android:title="#string/action_favorite"
app:showAsAction="ifRoom"/>
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
app:showAsAction="never"/>
</menu>
What you need to do basically is add xmlns:app="http://schemas.android.com/apk/res-auto"to the menu element in yout XML and use the showAsAction in the following format: app:showAsAction="ifRoom".
This will show the icon in the action bar, if possible.
How do I change the Android search view icon color? By default color is white, is it possible to change the icon color.
I have tried:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/action_search"
android:actionViewClass="android.widget.SearchView"
android:icon="#drawable/ic_theme_blue_menu"
android:showAsAction="always"
android:title="#string/search_actionbar"/>
<item
android:id="#+id/slider_menu"
android:icon="#drawable/ic_slider_menu"
android:orderInCategory="100"
android:showAsAction="always"/>
</menu>
But I need to change it in code, not in the layout file. Is it possible?
Unfortunately, there is no easy way to change the SearchView icon to a custom drawable, since the theme attribute searchViewSearchIcon is not public. Check this answer for details.
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>
What I do, I have create a method to change any drawable color
public Drawable changeDrawableTint(Drawable drawable, int color){
final Drawable wrappedDrawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTintList(wrappedDrawable, ColorStateList
.valueOf(color));
return wrappedDrawable;
}
, even in android versions pre lollipop, then I get the search view menu item, and call the method with the desired color
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
/**
* We inflate the correct activity menu
*/
getMenuInflater().inflate(R.menu.menu_partner_list, menu);
searchViewMenuItem = menu.findItem(R.id.action_search);
if(searchViewMenuItem!=null && searchViewMenuItem.getIcon()!=null){
searchViewMenuItem.setIcon(Utils.getInstance()
.changeDrawableTint(searchViewMenuItem.getIcon(),
Color.WHITE));
}
setUpSearchView(menu);
shouldHideMenuActions();
return true;
}
here is the menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always|collapseActionView"
android:icon="#drawable/ic_search"
android:title="#string/search" />
</menu>
you could use the following code in your java class:
mView.getBackground().setColorFilter(Color.parseColor("#ffffff"), PorterDuff.Mode.LIGHTEN);
This line above changes the color of the view to to a share of color you suggest in Color.parseColor() and a shade like how you define it using PorterDuff.Mode. Call the above code on click of a button to check if the color change is taking effect.
I have a working SearchView which expands in my OptionsMenu when the user taps on the search icon. However it only expands within the available space among the other OptionsMenu icons. On a wide screen this is fine, but with a narrow space there is only room to show 5-10 charaters in the search box. I want it to overlay the other icons such as it does for the Android Contacts app. Currently, I'm building with targetSdkVersion = 17. Hopefully I'm missing something simple :)
(Note added later: the only solution I've found workable so far is to hide all the menu icons when I want to expand the search icon. This is conceptually simple. But it is messy because when restoring hidden icons, one has to go through a bunch of logic to figure out which ones to restore, or keep state variables around, etc.)
Here's my item xml in for the OptionsMenu:
<item
android:id="#+id/menu_search_shallow"
android:title="Search Current Folder"
android:icon="#drawable/ic_btn_search"
android:showAsAction="always|collapseActionView"
android:actionViewClass="android.widget.SearchView" />
I also have in my main activity code:
#Override
public boolean onCreateOptionsMenu (Menu menu)
{
getMenuInflater().inflate(R.menu.nav_menu, menu);
this.optionsMenu = menu;
MenuItem searchItem = menu.findItem (R.id.menu_search_shallow);
searchItem.setOnActionExpandListener (this);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setQueryHint (getString (R.string.search_shallow_hint));
searchItem = menu.findItem (R.id.menu_search_deep);
searchItem.setOnActionExpandListener (this);
searchView = (SearchView) searchItem.getActionView();
searchView.setQueryHint (getString (R.string.search_deep_hint));
}
and
#Override
public boolean onMenuItemActionExpand(MenuItem item)
{
SearchView searchView = (SearchView) item.getActionView();
searchView.setOnQueryTextListener (this);
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item)
{
SearchView searchView = (SearchView) item.getActionView();
searchView.setQuery ("", false);
return true;
}
Instead of creating a new layout I set the MaxWidth programmatically in onCreateOptionsMenu():
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_search, menu);
SearchView searchView = (SearchView)menu.findItem(R.id.menu_search).getActionView();
searchView.setMaxWidth(Integer.MAX_VALUE);
....
For some reason jjung's solution didn't work for me. I had to add android:maxWidth="10000dp", just a really high maxWidth, to get it to expand to the full available width.
Also, just to add, I'm using the support library (v7), and so my layout file looks like this:
<android.support.v7.widget.SearchView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxWidth="10000dp" />
And in my item, I have:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto">
<item
...
myapp:actionLayout="#layout/searchview" />
...
Where myapp is just some arbitrary namespace for the attributes added by the support library (see Adding Action Items for more details).
Maybe I am late but thought it might be helpful for someone who faced the same issue as me.
final SearchView searchView = (SearchView) myActionMenuItem.getActionView();
searchView.setMaxWidth(Integer.MAX_VALUE);//set search menu as full width
in onCreateOptionsMenu
of your class may solve this problem but one thing we need to understand.
If you just want to show search menu (on clicking of search button) and not any other menu items in actionbar then you need to keep the search item as a first item in menu file. Here is an example
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!--keeping searach as first item-->
<item
android:id="#+id/action_search"
android:icon="#drawable/search_ab"
android:title="#string/search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom" />
<item
android:id="#+id/itemFilter"
android:icon="#drawable/filter"
android:title="#string/filter"
app:showAsAction="ifRoom" />
</menu>
And it will be displayed as
on clicking search it will be shown as full width
And if you keep search as second or last item in menu then it will be show like this.
and on clicking search
As crazy as it might seem, in my case problem was this line:
app:actionViewClass="android.widget.SearchView" in menu.xml:
<item
android:id="#+id/action_search"
app:actionViewClass="android.widget.SearchView"
android:icon="#drawable/ic_search"
android:title="#string/title"
app:actionLayout="#layout/layout_search"
app:showAsAction="always" />
When I removed app:actionViewClass, it fully expanded as defined in my layout_search file.
<android.support.v7.widget.SearchView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:maxWidth="10000dp"
android:layout_centerInParent="true"
android:background="#color/colorAccent"
android:queryHint="Search me" />
I had a similar issue, that the search bar did not fill the whole width,( but I had no other icons).
I solved it by adding in item:
android:actionLayout="#layout/my_search_view"
and in layout/my_search_view.xml :
<?xml version="1.0" encoding="utf-8"?>
<SearchView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
There are so many ways to solve this issue - I'll just add the one I've used in several apps.
I have my custom SearchView (with some other customization) which extends android.support.v7.widget.SearchView and in order to stretch to full width:
#Override
public void setLayoutParams(final ViewGroup.LayoutParams params) {
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
super.setLayoutParams(params);
}
I have my own theme for ActionBarSherlock based on Theme.Sherlock.Light.DarkActionBar, here are my styles:
<style name="Theme.MyTheme" parent="Theme.Sherlock.Light.DarkActionBar">
<item name="actionBarStyle">#style/Widget.MyTheme.ActionBar</item>
</style>
<style name="Widget.MyTheme.ActionBar" parent="Widget.Sherlock.Light.ActionBar.Solid.Inverse">
<item name="android:background">#drawable/action_bar</item>
<item name="background">#drawable/action_bar</item>
</style>
Where #drawable/action_bar is an xml:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle">
<solid android:color="#color/color_action_bar_bottom_line" />
</shape>
</item>
<item android:bottom="2dp">
<shape android:shape="rectangle">
<solid android:color="#color/color_action_bar_background" />
</shape>
</item>
</layer-list>
Everything looks great except one thing: the text in the SearchView (when I use the search mode in ActionBar) is "dark on dark", so I cannot see anything. I tried to set the text colors for SearchView manually:
AutoCompleteTextView searchTextView =
(AutoCompleteTextView) searchView.findViewById(R.id.abs__search_src_text);
searchTextView.setTextColor(Color.WHITE);
searchTextView.setHintTextColor(Color.LTGRAY);
searchTextView.setHighlightColor(Color.WHITE);
searchTextView.setLinkTextColor(Color.WHITE);
This helped a lot but I cannot change the color of autocomplete text:
As you see, it's still black. How can I set the white text color for this kind of text?
Try this code
SearchView searchView = new SearchView(context);
LinearLayout linearLayout1 = (LinearLayout) searchView.getChildAt(0);
LinearLayout linearLayout2 = (LinearLayout) linearLayout1.getChildAt(2);
LinearLayout linearLayout3 = (LinearLayout) linearLayout2.getChildAt(1);
AutoCompleteTextView autoComplete = (AutoCompleteTextView) linearLayout3.getChildAt(0);
autoComplete.setTextColor(Color.WHITE);
or
SearchView searchView = new SearchView(context);
AutoCompleteTextView search_text = (AutoCompleteTextView) searchView.findViewById(searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null));
search_text.setTextColor(Color.WHITE);
It's from my own answer here How to set SearchView TextSize?
I solved this by disabling autocomplete feature (it will be disabled if you'll set the edit type to Visible Password), but this solution is not very good and I'm still in search for better one.
private SearchView getSearchView() {
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
searchView.setQueryHint("Search for items");
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
AutoCompleteTextView searchTextView =
(AutoCompleteTextView) searchView.findViewById(R.id.abs__search_src_text);
if (searchTextView != null) {
searchTextView.setInputType(InputType.TYPE_CLASS_TEXT
| InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
searchTextView.setTypeface(Typeface.DEFAULT);
}
return searchView;
}
I think your are using a Light Theme with a custom dark action bar theme. You could try to change the theme of the AutoCompleteTextView to a dark one. Try to add this in your theme :
<style name="Widget.MyTheme.ActionBar" parent="Widget.Sherlock.Light.ActionBar.Solid.Inverse">
<item name="searchAutoCompleteTextView">#style/Widget.Sherlock.SearchAutoCompleteTextView</item>
<item name="android:searchAutoCompleteTextView">#style/Widget.Sherlock.SearchAutoCompleteTextView</item>
<item name="searchAutoCompleteTextViewStyle">#style/Widget.Sherlock.SearchAutoCompleteTextView</item>
<item name="android:searchAutoCompleteTextViewStyle">#style/Widget.Sherlock.SearchAutoCompleteTextView</item>
<item name="textAppearanceLargePopupMenu">#style/Widget.Sherlock.SearchAutoCompleteTextView</item>
<item name="android:textAppearanceLargePopupMenu">#style/Widget.Sherlock.SearchAutoCompleteTextView</item>
<item name="textAppearanceSmallPopupMenu">#style/Widget.Sherlock.SearchAutoCompleteTextView</item>
<item name="android:textAppearanceSmallPopupMenu">#style/Widget.Sherlock.SearchAutoCompleteTextView</item>
</style>
Edit : not sure about the added lines but you can try. I spot it in some fixes for the HoloEverywhere library.
May be there are is best way to do this , but used the very simple way, I used library for that
#Override
public boolean onCreateOptionsMenu(Menu menu) {
searchMenuItem = CollapsibleMenuUtils.addSearchMenuItem(menu, false,
textWatcher);
return true;
}
set the boolean value for true/false for setting Light/Dark theme
addSearchMenuItem(Menu menu, boolean isLightTheme, TextWatcher
textWatcher)
Adding collapsible search menu item to the menu.
Parameters: menu isLightTheme - true if use light them for ActionBar,
else false textWatcher
for changing the text color i changed the librery's layout/ holodark.xml
<AutoCompleteTextView
android:id="#+id/search_src_text"
android:layout_width="0dp"
.
.
android:textColor="#android:color/white" />
Here iam useing SearchView like this it's working nice searchtext color also in white color.
this one res/menu and name is applications_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/Accounts"
android:icon="#drawable/ic_launcher">
</item>
<item
android:id="#+id/menu_search"
android:actionViewClass="android.widget.SearchView"
android:icon="#drawable/ic_launcher"
android:showAsAction="ifRoom"
android:title="menu_search">
</item>
</menu>
this one use in activity when you search like this
new MenuInflater(this).inflate(R.menu.applications_menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
here is screen
The issue is that the SearchView is initialized without properly considering the ActionBar style (in this case Dark ActionBar on Light) that is set. And the answer is to use the ActionBar themed Context; retrieved by Activity.getActionBar().getThemedContext(). This is mentioned at the bottom of ABS Migration:
If you are using list navigation pay special attention to the 'List Navigation' example in the demos sample for direction on proper use. You need to use the library-provided layouts for both the item view and dropdown view as well as use the themed context from the action bar.
My code is how it is done in standard Android, you'd have to get the ActionBar however appropriate. If you pass the themed Context to the SearchView then it will use the appropriate style, namely, it won't render itself as if it were on a light background.
I want to disable the Mag icon displayed inside the search view component. Any idea how to reference it and remove it or replace it with another drawable ?
Since the wording of the question does not contain any mention of ActionBarSherlock (except for the tag) and comments have been added that the accepted answer does not work for standard and/or support library action bar, I'm posting another answer. Here is the Java code for onCreate:
// obtain action bar
ActionBar actionBar = getSupportActionBar();
// find SearchView (im my case it's in a custom layout because of left alignment)
View v = actionBar.getCustomView();
SearchView searchView = (SearchView)v.findViewById(R.id.search_view);
ImageView icon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
// method 1: does not work persistently, because the next line
// should be probably called after every manipulation with SearchView
// icon.setVisibility(View.GONE);
// method 2: working code
icon.setAdjustViewBounds(true);
icon.setMaxWidth(0);
icon.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
icon.setImageDrawable(null);
According to this post, while using a SearchView from the support library (android-support-v7-appcompat in my case) with setIconifiedByDefault(false) the icon is displayed outside edit textbox (SearchAutoComplete). Otherwise (setIconifiedByDefault(true)) it's displayed inside the box. The presented code is used for disabled "iconification" by default, and may require some changes to work with "iconified" search view. I don't know if the same applies to ActionBarSherlock.
Hope this helps.
This icon is hint. So you can simply set new hint text.
int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
EditText searchPlate = (EditText) searchView.findViewById(searchPlateId);
searchPlate.setHint("Search");
If you want icon as hint text use spanable string with ImageSpan
int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
EditText searchPlate = (EditText) searchView.findViewById(searchPlateId);
searchPlate.setTextColor(getResources().getColor(R.color.search_text_color));
SpannableString string = new SpannableString(" ");
Drawable d = getResources().getDrawable(R.drawable.ic_search);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BOTTOM);
string.setSpan(span, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
searchPlate.setHint(string);
In your theme:
<style name="Theme" parent="Your parent theme">
<item name="android:searchViewSearchIcon">#android:drawable/ic_search</item>
</style>
Edit:
searchViewSearchIcon is a private attribute. This answer therefore does not work (on the native ActionBar).
You have to add this line in your MenuItem android:iconifiedByDefault="#android:color/transparent"
<item
android:id="#+id/activity_home_action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/activity_home_search_title"
android:iconifiedByDefault="#android:color/transparent"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always|collapseActionView" />
Or you can make programatically searchView.setIconifiedByDefault(true);
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
getMenuInflater().inflate(R.menu.home, menu);
MenuItem searchMenuItem = menu.findItem(R.id.activity_home_action_search);
SearchView searchView = (SearchView) searchMenuItem.getActionView();
searchView.setIconifiedByDefault(true);
}
To remove the hint search button:
mNearMeSearchBar = (SearchView) mView.findViewById(R.id.nearme_search_component_nearme_edittext);
mNearMeSearchBar.setIconifiedByDefault(false); //Removes Search Hint Icon
To change the search button drawable:
int searchImgId = getResources().getIdentifier("android:id/search_mag_icon", null, null);
ImageView v = (ImageView) mNearMeSearchBar.findViewById(searchImgId);
v.setImageResource(R.drawable.ic_compass);
You have to customize the SearchView style like this:
<style name="MySearchViewStyle" parent="Widget.AppCompat.SearchView">
<!-- Search button icon -->
<item name="searchIcon">#drawable/ic_arrow_back</item>
</style>
And then add it in your AppTheme.
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- your other colors and styles -->
<item name="searchViewStyle">#style/MySearchViewStyle</item>
</style>
And apply it to your search view using the tag style:
<android.support.v7.widget.SearchView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/searchView"
android:layout_height="wrap_content"
android:layout_width="match_parent"
app:queryHint="#string/search_hint"
android:focusable="true"
android:focusableInTouchMode="true"
style="#style/MySearchViewStyle"
app:iconifiedByDefault="false" />
Hope it helps!
If you are using the v7 app compat library you can easily change this in the theme. In your theme that extends from an AppCompat theme just add a line like:
<item name="searchViewSearchIcon">#drawable/ic_search</item>
These lines removed the magnifying glass:
//remove search icon
mSearchView.setIconifiedByDefault(false);
ImageView icon = (ImageView) mSearchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
icon.setVisibility(View.GONE);
In your theme style include this:
<style name="YourTheme.Toolbar">
<item name="searchViewStyle">#style/YourActionBarSearch</item>
<item name="editTextColor">#color/your_ab_text_color</item>
<item name="android:textColorHint">#color/your_ab_hint_color</item>
</style>
You can now define the style of the SearchView with searchViewStyle, and editTextColor will set the color of the text in the search box. By setting android:textColorHint you will also set the color of the hint, e.g. "Search...".
<style name="YourActionBarSearch" parent="#style/Widget.Holo.SearchView">
<item name="searchIcon">#drawable/ic_ab_search</item>
<item name="queryBackground">#null</item>
<item name="submitBackground">#null</item>
<item name="searchHintIcon">#null</item>
<item name="defaultQueryHint">#null</item>
<item name="closeIcon">#drawable/ic_ab_small_search_close</item>
</style>
This will style your search view as you like, more or less. The field searchIcon is the icon in the action bar. The field searchHintIcon is the small icon to the left of the hint in the search field, which you are asking for in your question; set it to #null to remove the icon. The field closeIcon is the close icon at the right side of the search field.
In xml in searchView component add next :
app:searchHintIcon="#null"