Android SearchView Icon - android

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"

Related

How to change searchview icon color?

I know there's already a lot of answers for this question here, and I wouldn't be creating another thread if I hadn't already tried everything I saw here.
Anyway, the question is pretty simple. I don't mind if the solution comes from XML styling or Java code (both solutions are welcome). I will show everything (most of it anyway) I've tried to solve it.
First of all, the searview icon seems to be immutable by code since I can't change anything from it. My searchview is declared in menu.xml as this
<item android:id="#+id/icon_search"
android:title="Searchador"
android:icon="#drawable/ic_search_black_24dp" <!-- icon not used by searchview anyway -->
android:showAsAction="always"
android:actionViewClass="android.widget.SearchView"
/>
and inside my onCreateOptionsMenu I have this
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchMenuItem = menu.findItem(R.id.icon_search);
searchView = (SearchView) searchMenuItem.getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setSubmitButtonEnabled(false);
searchView.setOnQueryTextListener(this);
MenuTint.colorIcons(this, menu, Color.BLUE);
return true;
The MenuTint.colorIcons(this, menu, Color.BLUE); I got from a special class I found here where it basically changes all my icons color, unfortunately not working for the searchview icon.
I then found some answers suggesting to use a style like this
<style name="SearchViewStyle" parent="Widget.AppCompat.SearchView">
<!-- changing my icon, its color and stuff.. -->
<item name="searchIcon">#drawable/ic_search</item></style>
but I was getting the No resource found that matches the given name 'Widget.AppCompat.SearchView'. error.
I then found that I should add the android-support-app-compat project library instead of just the .jar. It didn't work. I mean, the import worked, but the error kept showing. Then, I found somewhere that I should change the project.properties from target=android-19 to 21 (or higher) but it didn't solved the "No resource found" error.
So I'm pretty much stuck for this simple detail where I need to change the searchview color.
Also, this is not exactly the same question, but I believe it might be solved the in the same way so I will include here: I wanted to change distance between the icons. Someone suggested this solution
<style name="ActionButtonStyle" parent="AppBaseTheme">
<item name="android:minWidth">0dip</item>
<item name="android:paddingLeft">0dip</item>
<item name="android:paddingRight">0dip</item>
</style>
but I end up getting this
As you can see, this approach works for my custom icons only, but not for the searchview icon, neither for the menu overflow icon. I would like to change the distance between ALL icons equally.
If you want to change SearchView's search icon, you just need to get the image view within the view as follow:
searchView = v.findViewById(R.id.searchView);
//change icon color
ImageView searchIcon = searchView.findViewById(android.support.v7.appcompat.R.id.search_button);
searchIcon.setImageDrawable(ContextCompat.getDrawable(getActivity(),R.drawable.ic_search_icon));
It is important that the icon is R.id.search_button and you can replace it with a white vector asset that you provide, in this case R.drawable.ic_search_icon
Similarly, you can change the text within the SearchView as follows:
//set color to searchview
SearchView.SearchAutoComplete searchAutoComplete = searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchAutoComplete.setHintTextColor(getResources().getColorandroid.R.color.white));
searchAutoComplete.setTextColor(getResources().getColor(android.R.color.white));
For android.x,
#Override
public boolean onCreateOptionsMenu(Menu menu) {
...
ImageView searchIcon=
searchView.findViewById(androidx.appcompat.R.id.search_mag_icon);
// To change color of close button, use:
// ImageView searchCloseIcon = (ImageView)searchView
// .findViewById(androidx.appcompat.R.id.search_close_btn);
searchIcon.setColorFilter(getResources().getColor(R.color.colorPrimary),
android.graphics.PorterDuff.Mode.SRC_IN);
...
return true;
}
final SearchView searchViewAndroidActionBar = (SearchView) MenuItemCompat.getActionView(searchViewItem);
// change close icon color
ImageView iconClose = (ImageView) searchViewAndroidActionBar.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
iconClose.setColorFilter(getResources().getColor(R.color.grey));
//change search icon color
ImageView iconSearch = searchViewAndroidActionBar.findViewById(android.support.v7.appcompat.R.id.search_button);
iconSearch.setColorFilter(getResources().getColor(R.color.grey));
To change the search view's icon color use the following lines of code:
SearchView searchView = (SearchView) findViewById(R.id.searchview);
ImageView icon = searchView.findViewById(android.support.v7.appcompat.R.id.search_button);
icon.setColorFilter(Color.BLACK);
You can change or customize the icons of the SearchView by adding you're own drawable icons.
<androidx.appcompat.widget.SearchView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
app:closeIcon="#drawable/close_icon"
app:searchHintIcon="#drawable/search_icon"
app:searchIcon="#drawable/search_icon"
android:textStyle="bold" />
Use below code:
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView mSearchView = (SearchView) searchItem.getActionView();
Drawable dr = searchItem.getIcon();
dr.setColorFilter(ContextCompat.getColor(this, R.color.colorPrimary), PorterDuff.Mode.SRC_ATOP);
OR:
dr.setColorFilter(BlendModeColorFilterCompat
.createBlendModeColorFilterCompat(Color.RED, BlendModeCompat.SRC_ATOP));
Take the Drawable you want to tint, wrap it with a tinted drawable , and set it as a new one for the search menu item:
#JvmStatic
fun getTintedDrawable(inputDrawable: Drawable, #ColorInt color: Int): Drawable {
val wrapDrawable = DrawableCompat.wrap(inputDrawable.mutate())
DrawableCompat.setTint(wrapDrawable, color)
DrawableCompat.setTintMode(wrapDrawable, Mode.SRC_IN)
return wrapDrawable
}
Usage:
val drawable =getTintedDrawable(...)
searchMenuItem.icon = drawable
And this should work for all Android versions that android-x (support library) supports.
Use below code:
ImageView searchIcon = searchView.findViewById(R.id.search_button);
searchIcon.setColorFilter(Color.WHITE);
If you want to change close icon color, use this one:
ImageView searchClose = searchView.findViewById(R.id.search_close_btn);
searchIcon.setColorFilter(Color.WHITE);
I have found that in later (2020) appCompat scenarios I have had trouble changing icon colors, but found this one to work:
(kotlin)
searchItem.icon.colorFilter = BlendModeColorFilterCompat.createBlendModeColorFilterCompat(Color.RED, BlendModeCompat.SRC_ATOP)
You can use theme in your layout
In your styles file define:
<style name="YourAppTheme.DarkSearchViewTheme">
<item name="colorControlNormal">#FFFFFF</item>
</style>
Then if you use menu inflation, you can apply this theme into your activity from the manifest
Or if you explicitly use SearcView widget in your layout, you can apply it into your widget by theme attribute like this:
<androidx.appcompat.widget.SearchView
android:theme="#style/YourAppTheme.DarkSearchViewTheme"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

How to change SearchView textcolor?

My SearchView is located in a LinearLayout / RelativeLayout rather than the action bar.
Its default text color is white, how to change it?
Digging into the source code of the appcompat SearchView I found that it uses an AppCompatAutoCompleteTextView which by default uses the autoCompleteTextView style.
Creating a different autocomplete style and setting it in the app theme solved the problem for me.
<style name="SearchAutoCompleteTextView" parent="Widget.AppCompat.Light.AutoCompleteTextView">
<item name="android:textColor">#color/my_text_color</item>
<item name="android:textColorHint">#color/my_hint_color</item>
</style>
<style name="AppTheme" parent="Theme.AppCompat.Light">
...
<item name="autoCompleteTextViewStyle">#style/SearchAutoCompleteTextView</item>
</style>
For Androidx Library
SearchView searchView = (SearchView) findViewById(R.id.search);
EditText searchEditText = searchView.findViewById(androidx.appcompat.R.id.search_src_text);
searchEditText.setTextColor(getResources().getColor(R.color.white));
searchEditText.setHintTextColor(getResources().getColor(R.color.white));
For Android Support Library
SearchView searchView = (SearchView) findViewById(R.id.search);
EditText searchEditText = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchEditText.setTextColor(getResources().getColor(R.color.white));
searchEditText.setHintTextColor(getResources().getColor(R.color.white));
Try this,
SearchView searchView= (SearchView) findViewById(R.id.searchView1);
int id = searchView.getContext()
.getResources()
.getIdentifier("android:id/search_src_text", null, null);
TextView textView = (TextView) searchView.findViewById(id);
textView.setTextColor(Color.WHITE);
or
((EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text))
.setHintTextColor(getResources().getColor(R.color.white));
or
searchView.setQueryHint(Html.fromHtml("<font color = #ffffff>" +
getResources().getString(R.string.your_str) + "</font>"));
This may helps you.
For me worked next approach:
1.add style into your styles.xml
<style name="SearchViewStyle" parent="Widget.AppCompat.SearchView">
<item name="android:textColor">#color/white</item>
<item name="android:editTextColor">#color/white</item>
<item name="android:textColorHint">#color/light_gray</item>
</style>
2.add this line in your SearchView layout code
app:theme="#style/SearchViewStyle"
I would do this kind of job through the xml layout file. This can be found in the res/layout folder.
You would need to do something similar to the following:
Add this to the parent theme.
<item name="android:editTextColor">#android:color/white</item>
This should change the entered text.
You can also use something like this:
<item name="android:textColorHint">#android:color/white</item>
It will change the hint text for the SearchView.
Hope this helps :)
Just override the default color using the android:theme attribute:
<androidx.appcompat.widget.SearchView
android:theme="#style/ThemeOverlay.SearchView"
with
<style name="ThemeOverlay.SearchView" parent="">
<!-- Text color -->
<item name="android:editTextColor">#color/...</item>
<!-- Hint text color -->
<item name="android:textColorHint">#color/...</item>
</style>
Try this, if its not working try to change your theme.
((EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text)).setHintTextColor(getResources().getColor(R.color.white));
or try like this
searchView.setQueryHint(Html.fromHtml("<font color = #ffffff>" + getResources().getString(R.string.hintSearchMess) + "</font>"));
In this link colors of search icon, search hint icon, close icon, plate, query hint color, query color are changed
This worked for me:
EditText searchEditText = searchView.findViewById(androidx.appcompat.R.id.search_src_text);
searchEditText.setTextColor(getResources().getColor(R.color.white));
searchEditText.setHint("Search");
searchEditText.setHintTextColor(getResources().getColor(R.color.white));

Remove Search View icon from hint text

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.

Change the action bar menu selected text color for appcompat

I have been having a lot of trouble trying to set the action bar text color using android-support-v7-appcompat. I was able to change the selector color from the default color to a light grey however now when I select something the text color changes as well. I want the text color to stay black when the user makes a selection. Any ideas? Thanks!
in my themes.xml, this did the trick
<style name="AppTheme.AppCompat.Light" parent="#style/Theme.AppCompat.Light">
<item name="android:itemTextAppearance">#style/MenuTextAppearance</item>
</style>
<style name="MenuTextAppearance">
<item name="android:textColor">#android:color/black</item>
</style>
Additionally, I found you could use a spannable string to customize the text color of the menuItems programmatically. This caused the app to crash when using appcompay-v7 however others have had success with Sherlock, Holo, etc. Example:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.your_menu, menu);
int positionOfMenuItem = 0; // or whatever...
MenuItem item = menu.getItem(positionOfMenuItem);
SpannableString s = new SpannableString("My red MenuItem");
s.setSpan(new ForegroundColorSpan(Color.RED), 0, s.length(), 0);
item.setTitle(s);
}
https://stackoverflow.com/a/19008593/2820963
This would be great if you want different menu items to have different colors.
I believe there is a style for select items so you will need to explicitly define the text color for when an item is selected or your app will revert to the default textcolor. The attribute should be something like android:textColorHighlight

How to change the autocomplete text color for search view inside of ActionBarSherlock with dark background?

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.

Categories

Resources