In my application I am using the Action Bar with a custom SearchView in it. I want to use some custom code when the user hits the enter button in the search field. But I cant find a way to catch the moment the user hits the enter button in the SearchView.
Now I tried several ways to capture the search action but I cant seem to find it.
My button is defined like this:
<item
android:id="@+id/action_bar_search"
android:title="Search"
android:icon="@drawable/ic_menu_search"
android:showAsAction="always"
android:actionViewClass="-package-.CustomSearchView"
/>
With this belongs the CustomSearchView class:
public class CustomSearchView extends SearchView implements OnClickListener, android.view.View.OnKeyListener{
public CustomSearchView(Context context) {
super(context);
this.setOnSearchClickListener(this);
this.setSubmitButtonEnabled(true);
this.setOnClickListener(this);
this.setOnKeyListener(this);
}
@Override
public void onClick(View v) {
Log.d("SEARCH", "Onclick! " + this.getQuery());
}
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
Log.d("SEARCH", "Search onkey");
return false;
}
}
Here you can see that I`ve tried 2 approaches:
- Catch the onclick event from the submit button. That didnt work since it only fired when I clicked on the search icon to open the SearchView.
- Catch keystrokes within the SearchView. That didnt work either. Got no response from any key that I pressed there.
Does anyone have a way of making a custom Search action with this SearchView?
UPDATE:
As stated below by PJL: You dont need a custom dialog for overriding this behaviour. The normal SearchView widget will work just fine for this.