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);
Related
I've followed the code on http://www.camposha.info/source/android-recyclerview-search-filter and made my search filter work. The only thing I need is when opening the activity to keep the Cardview hidden, or invisible until the search has started in the searchbar (in other words, to keep the Cardview hidden until I start typing something in the searchbar).
I have read the comments which I should have edited the Edittext but I don't have it. what should I do?
Keep the cardview's visibility gone in the layout. And add a queryTextListener to your searchView
yourSearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String newText) {
if(newText.length()>0){
yourCardView.setVisibility(View.VISIBLE);
}else{
yourCardView.setVisibility(View.GONE);
}
return true;
}
#Override
public boolean onQueryTextSubmit(String query) {
return true;
}
}
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 have a search view which is set as expanded by default with default search query but i don't want the virtual keyboard.In below code i tried to hide keyboard in onCreateOptionsMenu but still keyboard is visible.
imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
MenuItem item = menu.findItem(R.id.menu_search);
item.expandActionView();
mSearchView = (SearchView) item.getActionView();
mSearchView.setIconifiedByDefault(false);
mSearchView.setQuery(query, true);
imm.hideSoftInputFromWindow(mSearchView.getWindowToken(), 0);
I am using sherlock search view widget. any suggestion to hide the virtual keyboard.What i am doing wrong?
Inspired by Parnit's answer, I've found a better method, which also works and is more beautiful:
mSearchView.clearFocus();
Edit: I added the better solution on top, but also kept the old answer as a reference.
#Override
public boolean onQueryTextSubmit(String query) {
searchView.clearFocus();
return false;
}
Original Answer: I programmed using a setOnQueryTextListener. When the searchview is hidden the keyboard goes away and then when it is visible again the keyboard does not pop back up.
//set query change listener
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){
#Override
public boolean onQueryTextChange(String newText) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean onQueryTextSubmit(String query) {
/**
* hides and then unhides search tab to make sure keyboard disappears when query is submitted
*/
searchView.setVisibility(View.INVISIBLE);
searchView.setVisibility(View.VISIBLE);
return false;
}
});
try
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
add the below line in the manifest for particular Activity.
android:windowSoftInputMode="adjustPan|stateHidden"
simple solution its work for my
add to XML:
android:focusable="false"
In Android Manifest:
android:windowSoftInputMode="adjustPan|stateHidden"
In class open and close the keyboard:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action buttons
switch(item.getItemId()) {
case R.id.search:
//TODO Whatever
search.clearFocus();
//Open and close the keyboard
InputMethodManager imm = (InputMethodManager)MyApplication.getAppContext().getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
return true;
u just have to use:
"object(edittext, searchview, etc)".clearfocus() ;
use it after u generate a search or an action. Example: in the method OnQueryTextListener, after that i use a search. For searchview.
We've got a SearchView on the ActionBar which is set to be non-iconified. As we don't have any content in the view until the user's entered something to search for, it would make sense to give the SearchView initial focus, and make sure the soft keyboard is showing ready for the user to enter text — otherwise they'll always have to first tap in the SearchView.
I can give the SearchView focus by just calling
searchView.requestFocus();
but I can't get the soft keyboard to appear. In another one of our Fragments I have an EditText which we want to be focused I can get the soft keyboard to appear there by calling
InputMethodManager mgr = (InputMethodManager)getActivity().getSystemService(
Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
but this just doesn't work on the SearchView. It must surely be possible to get this to work.
Further rummaging around StackOverflow and I found this question:
Forcing the Soft Keyboard open
which contains a solution that worked for me:
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).
toggleSoftInput(InputMethodManager.SHOW_FORCED,
InputMethodManager.HIDE_IMPLICIT_ONLY);
I have a similar problem where none of the proposed solutions here worked. Some just didn't make the keyboard appear at all and some show a keyboard but the key presses there just do not work.
The only thing that worked was:
// hack for making the keyboard appear
searchView.setIconified(true);
searchView.setIconified(false);
I am using a SearchView with setIconifiedByDefault(false). Testing with Android 4.4.2, the only way I could get the keyboard to actually show was to look at the source code for SearchView and mimic how it requested the keyboard to be shown. I've tried literally every other method I could find/think of and this is the only way I could get the keyboard to show reliably. Unfortunately, my method requires some reflection.
In onCreateOptionsMenu(Menu):
searchView.requestFocus();
searchView.post(new Runnable() {
#Override
public void run() {
showSoftInputUnchecked();
}
});
And then create a method to call the hidden method "showSoftInputUnchecked" in InputMethodManager:
private void showSoftInputUnchecked() {
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
Method showSoftInputUnchecked = null;
try {
showSoftInputUnchecked = imm.getClass()
.getMethod("showSoftInputUnchecked", int.class, ResultReceiver.class);
} catch (NoSuchMethodException e) {
// Log something
}
if (showSoftInputUnchecked != null) {
try {
showSoftInputUnchecked.invoke(imm, 0, null);
} catch (IllegalAccessException e) {
// Log something
} catch (InvocationTargetException e) {
// Log something
}
}
}
}
As with all solutions that access methods not in the public API, I can't promise that this won't break with new versions of Android.
It worked for me.
private SearchView mSearchView;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
MenuItem searchItem = menu.findItem(R.id.action_search);
mSearchView =
(SearchView) searchItem.getActionView();
mSearchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_search) {
mSearchView.setIconifiedByDefault(true);
mSearchView.setFocusable(true);
mSearchView.setIconified(false);
mSearchView.requestFocusFromTouch();
}
return super.onOptionsItemSelected(item);
}
If you wanna show the soft keyboard and focus on the input box, you can try
final MenuItem menuItem = menu.findItem(R.id.action_search);
menuItem.expandActionView();//expand show soft keyboard
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_actionbar_search"
liven:showAsAction="collapseActionView|always" //always
liven:actionViewClass="android.support.v7.widget.SearchView" />enter code here
This will not show keyboard every time you come on activity
searchview.clearFocus();
Use expandView method :
Your onCreateOptionsMenu could be something like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//Used to put dark icons on light action bar
final SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
MenuItem mitem = menu.add("Search");
mitem.setIcon(ic_search_inverse)
.setActionView(searchView)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
mitem.expandActionView();
listsearch.setOnItemClickListener(this);
return true;
}
You can try what I did. This worked well for me.
//set query change listener
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){
#Override
public boolean onQueryTextChange(String newText) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean onQueryTextSubmit(String query) {
/**
* hides and then unhides search tab to make sure keyboard disappears when query is submitted
* 4 = INVISIBLE
* 0 = VISIBLE
*/
searchView.setVisibility(4);
searchView.setVisibility(0);
return false;
}
});