Aiden Fry Aiden Fry - 1 month ago 25
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(R.id.menu_finder_text_search).getActionView();
_searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
_searchView.setIconifiedByDefault(false); // Do not iconify the widget, we want to keep it open!
_searchView.setFocusable(false);
_searchView.setClickable(true);

_searchView.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
//DO SOMETHING!
}
});


Anyone?

Answer

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;
                text.setFocusable(false);
            }
            child.setOnClickListener(listener);
        }
    }
}

from: http://www.trinea.cn/android/searchview-setonclicklistener-not-working/

Comments