Android - SearchView in toolbar - android

I want to add a searchview into the toolbar. I want to expand the search view always. The body of onCreateOprionsMenu callback is below:
SearchView searchView = new SearchView(context);
searchView.onActionViewExpanded();
searchView.clearFocus();
searchView.setIconified(false);
searchView.clearFocus();
menuItem = optionsMenu.add(0, id, 0, title);
menuItem.setEnabled(item.enabled);
menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menuItem.setActionView(searchView);
Screenshots are below:
Why the first close button of the searchview is visible but not other.?
Also after editing input text, I press the second close button and it collapsed the search view. Second clear button clears only text in edit text.
I couldn't fix this problem.

Related

How to replace the actionbar by a popup menu?

I want to achieve 1 of these options for my EditText :
Replace the actionbar that appear on the top by a popup menu instead. Something like this for exemple:
Or make the actionbar floating and child of my current view (in some way same a first option)
i need this because i add my view via windowManager.addView(view, Layout_Params); and in this way i have some trouble with the actionbar on the top (it is displayed blank)
actually i do this to show the actionbar :
#Override
public ActionMode startActionMode(ActionMode.Callback callback) {
Activity host = (Activity) this.getContext();
return host.getWindow().getDecorView().startActionMode(callback);
}
but it's don't work, it's show me an empty white actionbar on the stop instead :( i think i need to create myself the ActionMode but i don't know how to do it.
Ok. You can hide the actionbar when your edittext is gained focus. Then you need to show the popup menu where ever you want.
EditText t = new EditText(this);
t.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if(b){
getSupportActionBar().hide();
//now show your popup menu at the top position
}else{
getSupportActionBar().show();
//here you dismiss the menu.
}
}
});

How to detect a click anywhere on a SearchView

How to detect a click on searchView?
I have tried using setOnClickListener, setOnSearchClickListener, setOnFocusChangedListener, but to no avail.
The problem seems to be only click on textbox. If I click anywhere outside of textbox onClickListener triggers, but not if click inside the textbox.
The problem that I am trying to solve is that I need to close a if user clicks anywhere. But I dont know how to handle the SearchView.
I have managed to find a way to solve my problem.
It's a bit hackish, but it works. You have to get the editText from searchView and set OnClickListener to it
int id = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
EditText editText = (EditText) searchView.findViewById(id);
editText.setOnClickListener(listener);
Try this:
searchView = (SearchView)findViewById(R.id.searchView);
searchView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
searchView.setIconified(false);
}
});
Try adding app:iconifiedByDefault="false" to the SearchView tag in your layout XML.
An answer from a similar question: How to detect if SearchView is expanded?
searchView.setOnQueryTextFocusChangeListener { _ , hasFocus ->
if (hasFocus) {
// searchView expanded
} else {
// searchView not expanded
}
}

Change SearchView little magnifier icon

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

expand background of action bar menuItem to height of actionbar

In my app, I highlight a check button in the action bar when the user makes a change. I changed the background of the menuitem drawable, but it appears that the button does not expand the entire height of the actionbar (in the image below, what is currently being displayed is on the left; on the right is what I would like the button to look like)
How do I adjust the menuItem button so that its background expands the height of the action bar (and extends to the right edge of the screen)?
This is what I have:
if (myHorizontalLayout.getItemList().size() == 0) {
MenuItem check = menu.findItem(R.id.save);
check.setIcon(R.drawable.check);
} else{
MenuItem check = menu.findItem(R.id.save);
check.setIcon(R.drawable.check_highlight);
}

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

Categories

Resources