Change SearchView little magnifier icon - android

I want to change the little magnifier icon that looks like an EditText's drawableLeft on a SearchView.
I have tried the following:
This link shows the layout xml file for the whole Search Widget. So using this method (similar to reflection) I changed every icon excepting the little magnifier one:
int submitButtonId = searchView.getContext().getResources().getIdentifier("android:id/search_go_btn", null, null);
ImageView imageView = (ImageView) searchView.findViewById(submitButtonId);
imageView.setImageResource(submitIcon);
I tried changing both android:id/search_mag_icon and android:id/search_button, but the little magnifier inside the EditText is still gray (I just want a white one).
Still not beating, I proceed to make some hypothesis:
Since it didn't work it must be both ImageViews (corresponding to android:id/search_mag_icon and android:id/search_button) Then it must mean the icon is either a drawablePadding background or set up programatically.
"There is no android:drawableLeft nor android:drawableRight in the layout file so the first option is wrong...
Ergo, I proceed to confirm if it is indeed a background. NOPE, the background just covers the blue lines.
So, if the little magnifier is not an ImageView nor a drawableLeft/Right nor a background, It must have been set programmatically (Look for the nested class SearchAutoComplete, BUT NO!!!
So, how do I change this little magnifier icon? I have literally tried everything

Well this question took me about half an hour to write cause I wanted to rule out things I had already done, but I found the answer 5 mins after posting it: http://nlopez.io/how-to-style-the-actionbar-searchview-programmatically/
PS: yep, reflection all the way

In case someone is having this issue, What worked for me was:
searchView.setIconifiedByDefault(false); //This is what does the trick. Removing the magnifier icon.
ImageView searchHintIcon = (ImageView) searchView.findViewById(R.id.search_mag_icon);
searchHintIcon.setImageResource(R.drawable.ic_search_black);
Here is the whole method:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.home_search, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.search_action).getActionView();
//If, this one returns null, use second option.
SearchableInfo searchableInfo = searchManager.getSearchableInfo(getActivity().getComponentName());
if (searchableInfo == null) {
searchableInfo = searchManager.getSearchableInfo(new ComponentName(getApplicationContext(), SearchActivity.class));
}
searchView.setSearchableInfo(searchableInfo);
searchView.setIconifiedByDefault(false);
try {
EditText searchEditText = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchEditText.setTextColor(getResources().getColor(R.color.black));
searchEditText.setHintTextColor(getResources().getColor(R.color.black));
ImageView searchHintIcon = (ImageView) searchView.findViewById(R.id.search_mag_icon);
searchHintIcon.setImageResource(R.drawable.ic_search_black);
Field f = TextView.class.getDeclaredField("mCursorDrawableRes");
f.setAccessible(true);
f.set(searchEditText, 0);
} catch (Exception e) {
e.printStackTrace();
}
}

Related

Android SearchView Formatting

Spent far too long on this problem now. Have solved most of it, but still struggling with the search hint icon. Having searched extensively this following bit of code does work in my fragment
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
mSearchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
mSearchView.setIconifiedByDefault(true);
int id = mSearchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
mSearchViewTextView = (TextView) mSearchView.findViewById(id);
mSearchViewTextView.setTextColor(Color.WHITE);
mSearchViewTextView.setHintTextColor(Color.WHITE);
mSearchViewTextView.setBackgroundResource(R.drawable.action_textfield_search_alpha);
mSearchViewSsb = new SpannableStringBuilder(" ");
mSearchViewSsb.append(getResources().getString(R.string.fragment_friend_add_search_hint));
Drawable searchHintIcon = ContextCompat.getDrawable(MyApplication.getContext(), R.drawable.action_search);
int textSize = (int) (mSearchViewTextView.getTextSize() * 1.25);
searchHintIcon.setBounds(0, 0, textSize, textSize);
mSearchViewSsb.setSpan(new ImageSpan(searchHintIcon), 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mSearchViewTextView.setText(mSearchViewSsb);
int id1 = android.support.v7.appcompat.R.id.search_close_btn;
ImageView searchCloseIcon = (ImageView) mSearchView.findViewById(id1);
searchCloseIcon.setImageResource(R.drawable.action_close);
int id2 = android.support.v7.appcompat.R.id.search_button;
ImageView searchIcon = (ImageView) mSearchView.findViewById(id2);
searchIcon.setImageResource(R.drawable.action_search);
super.onCreateOptionsMenu(menu, getActivity().getMenuInflater());
mSearchView.setOnSearchClickListener(...);
mSearchView.setOnQueryTextListener(...);
}
However, it only works once when I call it in onCreateOptionsMenu. I have tried to reuse it in setOnQueryTextListener, but no matter what I do the application then either crashes or just ignores the change.
Also, when it is set once the curious thing is that the first time you have to press the cancel button twice, the first time you press the cancel button the SearchView TextView has all of the enhancements but the SearchHint Icon goes back to its original form.
All I need is that SearchHint icon to be white.
My theme is set to Theme.AppCompat.Light.DarkActionBar
If you just want to make the icon white you can add the following code to your menu file
<item
android:id="#+id/action_search"
android:icon="#drawable/abc_ic_search_api_mtrl_alpha"
android:title="#string/search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always|collapseActionView"/>

AppCompat SearchView icon can't be GONE

I'm using android.support.v7.widget.SearchView and i need to make this search icon GONE.
I have custom ActionBar layout with SearchView:
<android.support.v7.widget.SearchView
android:id="#+id/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
initialize SearchView :
searchView = (SearchView) findViewById(R.id.search);
AutoCompleteTextView searchText = (AutoCompleteTextView) searchView.findViewById(R.id.search_src_text);
searchText.setPadding(0, 0, 0, -padding);
searchText.setHintTextColor(getResources().getColor(R.color.light_gray));
View searchPlate = searchView.findViewById(R.id.search_plate);
searchPlate.setBackgroundResource(R.drawable.searchview_textfield);
View close = searchPlate.findViewById(R.id.search_close_btn);
close.setBackgroundResource(R.drawable.selectable_background_orange);
I was trying to customize searchIcon like this:
ImageView icon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
trying to set everything from here Android SearchView Icon but it's not that ImageView.
Can anyone help me plz?
public static void setSearchViewEditTextHint(Context context, SearchView searchView, int stringId){
int searchMagIconId = context.getResources().getIdentifier("android:id/search_src_text", null, null);
TextView textView = (TextView) searchView.findViewById(searchMagIconId);
if (null != textView) {
textView.setHint(stringId);
}
}
Function call:
setSearchViewEditTextHint(this, searchView, R.string.emptyString);
Hey Sorry I'm a little late to the party. I am having to do a similar thing. I need to change the icon. It's a bit of a hack but I think its the best possible solution. Looking in the support library code AOSP uses "Search_mag_icon" as the icon you are looking for. In your code you can do something like:
int searchMagIcon = getResources().getIdentifier("search_mag_icon", "id", "android");
ImageView SearchHintIcon = (ImageView) searchView.findViewById(searchMagIcon);
and that will save that ImageView. Then you can simply change it or set it to null in your case.
Edit There is also a mSearch.getSearchView().setIconifiedByDefault() as well as a mSearch.getSearchView().setIconified() that you can use. I find that you MUST set the bydefault one to see results.
Hope that helps. :-)

SearchView remove blue focus line and show cursor at certain position

I have been trying to remove the blue focus line in the SearchView .I am casting it to a AutoCompleteTextView so that I can use customized icons for my searchView.
int id = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
searchtextView = (AutoCompleteTextView) searchView.findViewById(id);
I have tried using
searchtextView.setBackGroundColor(0)
and manually setting the background to #0000000 in my xml but I can still seee the blue line at the bottom.
The second problem I am facing is that the cursor does not show up on the text initially.I want to display the cursor when nothing is entered in the searchview.I tried to do it programmatically by using
if (searchtextView.getText().toString().length() >= 0) {
searchtextView.setSelection(searchtextView.getText().toString().length());
}
which should display the cursor even when no text exist inside the searchView.I think it has something to do with the focus because when I type in 2-3 characters the cursor shows up automatically.
I was able to solve it by setting the background of the default searchview plate as follows
int searchPlateId = searchView.getContext().getResources()
.getIdentifier("android:id/search_plate", null, null);
View searchPlateView = searchView.findViewById(searchPlateId);
if (searchPlateView != null) {
searchPlateView.setBackgroundColor(Color.BLACK);
}
This article helped.article link
You can use
android:queryBackground="#android:color/transparent"
I was able to make it work by clearingfocus on the search edit text during the menu creation function. Code below:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
...
final MenuItem searchItem = menu.findItem(R.id.action_search);
if (searchItem != null) {
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
final SearchView view = (SearchView) searchItem.getActionView();
if(view !=null){
LinearLayout searchPlate = (LinearLayout)mSearchView.findViewById(R.id.search_plate);
if(searchPlate != null){
mSearchEditText = (EditText)searchPlate.findViewById(R.id.search_src_text);
if(mSearchEditText != null){
mSearchEditText.clearFocus(); // This fixes the keyboard from popping up each time
}
}
}
}
}

MenuItemCompat.collapseActionView always return false

I am using the SearchView according to this guide, and I tried to collapse the searchview like this:
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
mSearchView = (SearchView) MenuItemCompat.getActionView(item);
mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
expand = !Helper.isNullOrEmpty(placeHolder);
Log.d("map", "set mSearch view with expand:" + expand + ", queryStr:" + placeHolder);
if (expand) {
MenuItemCompat.expandActionView(item);
// mSearchView.setQuery(placeHolder, false);
} else {
boolean x = MenuItemCompat.collapseActionView(item);
Log.d("map", "coop view:" + x);
}
However I always get this:
coop view: false
What's the problem?
BTW, I am getting crazy by the internal SearchView's strange behavior. Do you guys use this or something else?
Update(Why I want to control the searchview befora manually)
The activity which hold the serchview is my core activity, that's to say most of the search related job will be passed to this activity.
User may enter the search parameter in other activity and start a new Intent to this activity to do the job, then I have to change the status of the searchview manually
This would appear to solve the issue:
How to completely collapse a SearchView after an item selected?
According to the above thread (tested ok for me), you have to do this:
MenuItemCompat.collapseActionView(searchItem);
searchView.onActionViewCollapsed();
If you're not using AppCompat library, you can do this:
searchItem.collapseActionView();
searchView.onActionViewCollapsed();

Changing the cursor color in SearchView without ActionBarSherlock

I am attempting to change the color of the blinking cursor on the SearchView widget in ICS+. I have tried the following:
Adding <item name="android:textCursorDrawable">#null</item> to my
theme
Adding a style for AutoCompleteTextViews to my theme and setting the textCursorAttribute of that style to #null
Setting android:textCursorDrawable="#null" directly on the SearchView
I read the answer here (Custom cursor color in SearchView), but there is not a non-ABS style for searchAutoCompleteTextView, so could not try this. I also looked for a Java method to set the text cursor drawable, but could not find one - I am modifying other aspects of the SearchView in Java and would be able to do so with the cursor if there were a method available.
I have customized the SearchView pretty extensively, but this one last change is keeping it from looking right - the cursor is white on a white background, so it is not easily visible. Any other ideas of things I can try?
Based on the comments and answers above I made an example of how this could look using reflection. This solves the problem in my app. Hope it saves someone else some time.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.entity_list_actions, menu);
final SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
final int textViewID = searchView.getContext().getResources().getIdentifier("android:id/search_src_text",null, null);
final AutoCompleteTextView searchTextView = (AutoCompleteTextView) searchView.findViewById(textViewID);
try {
Field mCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
mCursorDrawableRes.setAccessible(true);
mCursorDrawableRes.set(searchTextView, 0); //This sets the cursor resource ID to 0 or #null which will make it visible on white background
} catch (Exception e) {}
return super.onCreateOptionsMenu(menu);
}
That 0 could be any other resource ID like R.drawable.my_cursor
<style name="CustomTheme" parent="android:Theme.Holo.Light">
...
<item name="android:autoCompleteTextViewStyle">#style/SearchViewStyle</item>
...
</style>
<style name="SearchViewStyle" parent="#android:style/Widget.Holo.Light.AutoCompleteTextView">
<item name="android:textCursorDrawable">#drawable/action_mode_close</item>
</style>
As per earlier comment:
If you dig into the Android source code, you'll find that mCursorDrawableRes only gets set once in the 3-param constructor. Unfortunately that means there is no easy way to change it at runtime. Looks like your options may be limited to using reflection, or moving your custom SearchView into the android.widget package in order to access the package protected member.
This changes the text color and works also on Android 8:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu_search, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
SearchView.SearchAutoComplete theTextArea = (SearchView.SearchAutoComplete) searchView
.findViewById(R.id.search_src_text);
theTextArea.setTextColor(getResources().getColor(R.color.yourColor));
...

Categories

Resources