How can I get a button back working when searching in Android
I've searched for over 4 hours all over the internet for how to get the back button to work correctly when the search is open, but I just can't seem to get it.
Here's my search.
searchview = (SearchView) findViewById(R.id.searchView);
searchview.setOnClickListener(this);
@Override
public void onClick(View v) {
if (v.getId() == R.id.searchView){
//Cursor cursor = manager.obtenerIDColumnaServicio(searchview.getQuery().toString());
adapterOfDataBase.changeCursor(cursor);
}
}
Here is the xml:
<SearchView
android:layout_width="match_parent"
android:layout_height="63dp"
android:id="@+id/searchView"
android:iconifiedByDefault="true"
android:backgroundTint="#ff000000"
android:layout_marginRight="0dp" />
All this with a huge class using SQL and some methods for insert, delete, etc. tables and records. So my problem is that every time I click on the searchview it expands to write text, everything is fine, but when I click the button from android nothing happens.
I tried 2 options but none worked, if you could just help me ....
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) { //MEJORAR ESTO
if (keyCode == KeyEvent.KEYCODE_BACK) {
searchview.onActionViewCollapsed();
return true;
}
return super.onKeyDown(keyCode, event);
}
//None of this 2 methods are working. I tried them separately
@Override
public void onBackPressed(){
if (!this.searchview.isIconified()) {
this.searchview.setIconified(true);
} else {
super.onBackPressed();
}
}
source to share
When the SearchView button is open and back is clicked, you expect to see the searchView closed (aka setIconified (true)). This is the default behavior as per the google apps search implementation.
So all you have to do is something like this
mSearchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
mSearchView.setIconified(true);
}
}
});
source to share
Try to use OnActionExpandListener on MenuItem
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_search, menu);
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
MenuItem searchItem = menu.findItem(R.id.search);
SearchView searchView = null;
if (searchItem != null) {
searchView = (SearchView) searchItem.getActionView();
}
if (searchView != null) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(MainActivity.this.getComponentName()));
}
searchItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// TODO do your stuff when back button is pressed
return true;
}
});
return super.onCreateOptionsMenu(menu);
}
In case of using AppCompatSupportv7
MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// TODO do your stuff when back button is pressed
return true;
}
});
source to share