Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 668
Aiden Fry Avatar asked Dec 20 '22 09:12

Aiden Fry


2 Answers

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/

like image 193
Trinea Avatar answered Dec 23 '22 00:12

Trinea


Ok, it does not answer the problem it only avoids it. I have used this link to create a listener for when the keyboard is shown. This gives me an event at the right time for me.

https://stackoverflow.com/a/7423586/1312937

like image 25
Aiden Fry Avatar answered Dec 23 '22 00:12

Aiden Fry