SearchView: can't capture the close event - android

I use the ActionBarShellock 4.2. I think the OnCloseListener() would be called when I click the closebutton but no response when i did it.
mSearchView.setOnCloseListener(new OnCloseListener() {
#Override
public boolean onClose() {
Toast.makeText(mContext, "OnCloseListener", 1000).show();
return false;
}
});
I've tried to call the getChildAt(index) to get the closebutton. Then I think it's unsafe cause the SearchView is not my own code. So, how can I capture the close event? Did I do in the wrong way?
thanks in advance.

Just get your menuItem, then setOnActionExpandListener. Then override unimplents methods.
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
//do something on close
// you must return true
return true;
}
Hack done
good luck

Related

How do I capture action button back from SearchView?

how can I capture the back arrow command in Android SearchView.
need to hide the previously opened list
You can use setOnActionExpandListener of MenuItem
mSearchItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem menuItem) {
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem menuItem) {
//TODO Here you can do your list hiding stuff :D
return true;
}
});

setOnActionExpandListener cause searchview not to expand

Using setOnActionExpandListener causes SearchView not to expand. Why? And how can I fix it?
menu.findItem(R.id.action_search).setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return false;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
//TODO
return true;
}
});
Try returning true in onMenuItemActionExpand. Per the comments in OnActionExpandListener:
return true if the item should expand, false if expansion should be
suppressed.

simulate onOptionsItemSelected inside method call

I have an ImageView inside a xml layout. The ImageView has an onClick method.
android:onClick="onHomeClicked"
When the user clicks my image, I want that to in turn call onOptionsItemSelected inside the activity. How would I do that?
The following code gives null for homeMenuItem:
MenuItem homeMenuItem;
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
homeMenuItem = menu.findItem(android.R.id.home);
super.onPrepareOptionsMenu(menu);
return true;
}
public void onHomeClicked(View view) {
onOptionsItemSelected(homeMenuItem);
}
You have no MenuItem, whatever logic onOptionsItemSelected() would contain would fail (assuming there are multiple options).
Since you seem to have a need for shared code, move that piece of logic to its own method, then call that method from both onHomeClicked() and onOptionsItemSelected().
Eg
private void mySharedMethod (){
//implementation
}
public void onHomeClicked (View v){
mySharedMethod();
}
public boolean onOptionsItemSelected (MenuItem item){
switch (item.getItemId ()){
case android.R.id.home:
mySharedMethod();
return true;
default:
return super.onOptionsItemSelected (item);
}
}
Possible you have to declare method and call it from onOptionsItemSelected and from onHomeClicked
onOptionsItemSelected(MenuItem item){
switch(item.getItemId()) {
case R.id.btn:
youNewMethod();
break;
}
}
public void onHomeClicked() {
youNewMethod();
}
private void youNewMethod(){
// write your code here
}

How to check if the back button of the ActionBarSherlock was clicked?

I'm using the library ActionBarSherlock, and put a item with the property android:showAsAction="ifRoom|collapseActionView". How to check if the back button of the ActionBarSherlock was clicked? thanks!
you have to override
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
}
return true;
}
from the doc:
his hook is called whenever an item in your options menu is selected.
The default implementation simply returns false to have the normal
processing happen (calling the item's Runnable or sending a message to
its Handler as appropriate). You can use this method for any items for
which you would like to do processing without those other facilities.
the answer above works (Thanks). But for my code, this solution works best ...
#Override
public boolean onOptionsItemSelected(
com.actionbarsherlock.view.MenuItem item) {
item.setOnActionExpandListener(new OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
// running changes ...
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// running changes ...
return true;
}
});
return super.onOptionsItemSelected(item);
};

how to call onSearchRequested when pressing magnifying glass

I'm using a searchView and try to pass some data when someone invokes a search. So i overrode the onSearchReqested method. The problem is, this method isn't called when someone types in the SearchView and presses the magnifying glass on the keyboard. Instead the result activity is started.
How can i call the onSearchRequested method prior starting the result activity when someone presses the magnifying glass.
i tried this two but it didnt work.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.FLAG_EDITOR_ACTION) {
onSearchRequested();
return false;
}
return false;
}
adding a listener to the searchview didn't work either
searchView.setOnSearchClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
onSearchRequested();
}
});
Thanks in advance for the help.
setOnQueryTextListerner finally worked
searchView.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
onSearchRequested();
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
// TODO Auto-generated method stub
return false;
}
});

Categories

Resources