Aiden Fry Aiden Fry - 1 year ago 368
Android Question

SearchView setOnClickListener NOT WORkING

Can anyone see why this is not working..

My SearchView is in the ActionBar and is always shown. I want to know when a user PRESSES the searchview... not when it expands or gains focus.

This code sits within onCreateOptionsMenu

SearchView = _searchView;

SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
_searchView = (SearchView) menu.findItem(;
_searchView.setIconifiedByDefault(false); // Do not iconify the widget, we want to keep it open!

_searchView.setOnClickListener(new OnClickListener() {

public void onClick(View v) {


Answer Source

SearchView is inherited from LinearLayout, so we can setOnClickListener for each child, like this:

public static void setSearchViewOnClickListener(View v, OnClickListener listener) {
    if (v instanceof ViewGroup) {
        ViewGroup group = (ViewGroup)v;
        int count = group.getChildCount();
        for (int i = 0; i < count; i++) {
            View child = group.getChildAt(i);
            if (child instanceof LinearLayout || child instanceof RelativeLayout) {
                setSearchViewOnClickListener(child, listener);

            if (child instanceof TextView) {
                TextView text = (TextView)child;


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download