I have a SearchView widget in my app, and I want to ask some questions about making it custom. First of all, you can start search only by clicking on search icon, is there any way to make whole SearchView clickable?
Also, is there a way to make SearchView appear something like this when it is clicked?
It is now in this state:
Here is my code:
citySearch = (SearchView) findViewById(R.id.city_search_bar);
citySearch.setBackgroundResource(R.drawable.search_background);
citySearch.setOnSearchClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
citySearch.setIconifiedByDefault(true);
//citySearch.setIconified(true);
}
});
citySearch.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String text) {
((Filterable) cityListView.getAdapter()).getFilter().filter(text);
return false;
}
#Override
public boolean onQueryTextSubmit(String text) {
return false;
}
});
try
{
Field searchField = SearchView.class.getDeclaredField("mSearchButton");
searchField.setAccessible(true);
ImageView searchBtn = (ImageView)searchField.get(citySearch);
searchBtn.setImageResource(R.drawable.search_icon);
searchBtn.setScaleType(ScaleType.FIT_CENTER);
searchPlate.setBackgroundResource(R.drawable.search_background);
}
catch (NoSuchFieldException e)
{
Log.e("SEARCHSTYLEERROR",e.getMessage(),e);
}
catch (IllegalAccessException e)
{
Log.e("SEARCHSTYLEERROR",e.getMessage(),e);
}
By default the SearchView is 'iconified', which is displayed as a magnifying glass icon and only if the user clicks on the icon, then the edit field expands.
To enable the user to click anywhere on the SearchView and expand the input field. We just need to add a click listener and call setIconified(false) when the user clicks.
searchView = (SearchView)findViewById(R.id.searchView);
searchView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
searchView.setIconified(false);
}
});
By default the SearchView is 'iconified'.
So you should use this code in java:
SearchView searchView = (SearchView) findViewById(R.id.searchView);
searchView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
searchView.setIconified(false);
}
});
or use this in xml:
<SearchView
android:id="#+id/searchView"
android:iconifiedByDefault="false" />
this 2 ways is correct, but different in icons display.
Another simple way to do it by setting onActionViewExpanded().
searchView = (SearchView)findViewById(R.id.searchView);
searchView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
searchView.onActionViewExpanded();
}
});
To allow writing when clicking in the whole component, I'd recommend you declaring iconifiedByDefault="false" on the xml.
<SearchView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:iconifiedByDefault="false"/>
You can use this anywhere in the code: searchView.setIconifiedByDefault(false);
From Android SDK:
When the SearchView is used in an ActionBar as an action view for a
collapsible menu item, it needs to be set to iconified by default
using setIconifiedByDefault(true). This is the default, so nothing
needs to be done.
If you want the search field to always be visible, then call
setIconifiedByDefault(false).
Related
when I use searchview, when searchview is active it hides menu icon , but i need this menu when search active also. How prevnt it from hiding?
After searching it hides the red colored menu
if you don't want sort menu in search, you can hide it on searchview expand. If you will hide sort menu, option menu will stay exists.
Here is example
final MenuItem sortmenu = menu.findItem(R.id.action_sort);
searchView.setOnCloseListener(new android.support.v7.widget.SearchView.OnCloseListener() {
#Override
public boolean onClose() {
sortmenu.setVisible(true);
invalidateOptionsMenu();
return false;
}
});
searchView.setOnSearchClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sortmenu.setVisible(false);
}
});
My app contains a standard SearchView widget. As you can see from the code below, I am setting an OnClickListener, an OnCloseListener, and an OnQueryTextListener.
If I tap the SearchView, it initially responds as expected. If I enter text and press the search button the keyboard, the OnQueryTextListener fires correctly, and the keyboard is dismissed as per searchView.setIconified(true). However, if I now tap the SearchView again, the OnClickListener is not fired. The keyboard still appears and the field becomes editable, but my code in the OnClickListener is not executed.
If I use the "X" icon to close the search view after this, everything returns to normal. The next time I click on the SearchView, my listener is fired.
I have additional code that I'll need to execute every time the SearchView is clicked.
What could be causing the listener to not fire in this specific instance? Is there something else that I should be doing in OnQueryTextSubmit?
searchView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
filterLayout.setVisibility(View.VISIBLE);
searchView.setIconified(false);
System.out.println("on click");
}
});
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
#Override
public boolean onClose() {
filterLayout.setVisibility(View.INVISIBLE);
return false;
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
searchView.setIconified(true);
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
I found a half-answer to this problem. If I clear the focus of the searchView when the query is submitted, then when the user next taps the searchView, the OnQueryTextFocusChangeListener gets a call. It doesn't exactly answer my original question, but it's an acceptable workaround for my case.
I have a searchview in my application. I need to search some information when user writes some criteria and after this, need show result in ListView.
How is it possible to know, if the user pressed the search button on the keyboard or not?
I read about OnQueryTextListener, but I still can't understand how to handle the press of a button from the android keyboard.
sv is the searchView
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String s) {
//here is what you want
return false;
}
#Override
public boolean onQueryTextChange(String s) {
// here you write what to do while typing
return false;
}
});
I'm using a filtering SearchView. Everything works fine but, when you leave the keyboard to type and press a button, a kind of toast is shown. Anyone know how to remove it?
My code is:
searchView = (SearchView) view.findViewById(R.id.buscador_lineas_transporte);
searchView.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
if(TextUtils.isEmpty(newText)){
lvLineasTransporte.clearTextFilter();
}
else{
lvLineasTransporte.setFilterText(newText);
}
return true;
}
});
Thanks.
Solved
This keyboard appears when you implement the interface Filterable in your adapater.
I recommendto use "AutoCompleteTextView" instead of "SearchView".
AutoCompleteTextView searchView = (AutoCompleteTextView) view.findViewById(R.id.searchView);
I have implemented an android SearchView in ActionBar. When the SearchView gains focus, the close button [x] at the right shows up. I took a look at other android native apps, like Contacts and Gmail. The close button is not shown when the SearchView gains focus.
How to set my SearchView behave like that?
Setting searchView.setIconifiedByDefault(false) will disable collapsing the search view and also remove the close button.
I faced the same problem with android.support.v7.widget.SearchView and found a solution. First, in onCreateOptionsMenu, you can obtain a reference to the SearchView as well as its close button:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.search, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);
try {
Field searchField = SearchView.class.getDeclaredField("mCloseButton");
searchField.setAccessible(true);
mSearchCloseButton = (ImageView) searchField.get(mSearchView);
} catch (Exception e) {
Log.e(TAG, "Error finding close button", e);
}
}
Now you can try to modify the button. First I tried to use setVisibility(View.GONE) to hide the close button, but that doesn't work, because the SearchView resets the visibility of its close button when the user interacts with the SearchView. So my solution was to use a transparent drawable and disable the click of the close button:
if (mSearchCloseButton != null) {
mSearchCloseButton.setEnabled(false);
mSearchCloseButton.setImageDrawable(getResources().getDrawable(R.drawable.transparent));
}
This article helped me as well:
http://novoda.com/blog/styling-actionbar-searchview
However, this is quite hacky to be honest. It would be cleaner to grab the SearchView source from https://android.googlesource.com/platform/frameworks/support.git/+/master/v7/appcompat/src/android/support/v7/widget/SearchView.java and create your own version of SearchView that does the hide/show of the close button.
Update:
Google just announced AppCompat v21, which has styling improvements for the SearchView widget:
http://android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-pre.html
You can also use this to hide the close the button
ImageView closeBtn = (ImageView) searchView.findViewById(R.id.search_close_btn);
closeBtn.setEnabled(false);
closeBtn.setImageDrawable(null);
Posting for future visitors. Previous answers are old and not easy. All you have to do is to set null to app:closeIcon this way app:closeIcon="#null"
<androidx.appcompat.widget.SearchView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:closeIcon="#null" <!-- This simple solution -->
app:iconifiedByDefault="false" />
You can get a link to the button from a SearchView object (AppCompat v23.2.1):
searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
ImageView mCloseButton = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
Then you can assign a listener to the SearchView text changes (SearchView also changes the visibility of the button, but the listener will be executed afterwards and will override those changes):
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
mCloseButton.setVisibility(newText.isEmpty() ? View.GONE : View.VISIBLE);
return false;
}
});
And finally, a listener to hide the icon when SearchView is expanded from iconified state:
searchView.setOnSearchClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// hide "x" button if there is no text
String query = searchView.getQuery().toString();
mCloseButton.setVisibility(query.isEmpty() ? View.GONE : View.VISIBLE);
}
});
With AndroidX, android.support.v7.appcompat.R.id.search_close_btn won't work
This will do the trick,
app:closeIcon="#null"