SearchView setOnClickListener NOT WORkING
Asked Answered
N

7

5

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?

Neckwear answered 26/2, 2013 at 14:50 Comment(0)
F
8

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/

Fidelis answered 22/1, 2014 at 3:41 Comment(0)
N
1

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://mcmap.net/q/109497/-how-to-capture-the-quot-virtual-keyboard-show-hide-quot-event-in-android

Neckwear answered 26/2, 2013 at 17:23 Comment(0)
J
1

Try this:

1) Bind the view

@BindView(R.id.search) SearchView search;

2) In your onCreate(), write the following code.

search.setIconifiedByDefault(false);
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                search.setIconified(false);
            }
        });

3) And your SearchView should have this following attributes.

          <SearchView
                android:id="@+id/search"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_centerVertical="true"
                android:background="@drawable/rounded_corners_box"
                android:drawableLeft="@android:drawable/ic_menu_search"
                android:imeOptions="actionSearch"
                android:inputType="text"
                android:maxLines="1"
                android:queryHint="Search your item.."
                android:textColor="@android:color/black"
                android:textColorHint="@color/colorPrimary"
                app:defaultQueryHint="Select locality"/>

NOTE:

android:background="@drawable/rounded_corners_box" -- your custom border xml file. android:drawableLeft="@android:drawable/ic_menu_search" -- search icon from drawable file.

Julejulee answered 27/10, 2016 at 8:59 Comment(0)
S
1

Bind the Searchviews button to a custom ImageView and add the onClickListener there

ImageView searchButton = this.searchView.findViewById(android.support.v7.appcompat.R.id.search_button);    
searchButton.setOnClickListener(v -> {

    // Your code here

    //This is needed since you are overwriting the default click behaviour
    searchView.setIconified(false);
}); 
Salted answered 30/8, 2018 at 14:6 Comment(1)
For androidx use androidx.appcompat.R.id.search_buttonFarnesol
R
1

Recently stuck with this problem and found a simple solution.

searchView.setOnQueryTextFocusChangeListener(object : View.OnFocusChangeListener{
        override fun onFocusChange(p0: View?, p1: Boolean) {
            // Enter your code here
        }
    })

This method will be called when you will tap on search field and soft keyboard will appear.

Roadstead answered 24/4, 2021 at 18:26 Comment(0)
C
0

Use the interface OnTouchListener: http://developer.android.com/reference/android/view/View.OnTouchListener.html

This requires a tiny bit more implementation code, but gives superior control over the UI. This solution assumes the user will be using a touch screen to interact with the View.

Considerable answered 26/2, 2013 at 15:1 Comment(3)
This is what i originally thought, still this does not respond when the user touches the searchview _searchView.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return false; } });Neckwear
Oh well. Could it be a nested View grabbing the touch events?Considerable
it looks like the search view swallows all touch/click events.Neckwear
M
0
int search_button_id = context.getResources().getIdentifier("android:id/search_button", null, null);
ImageView search_button_view = (ImageView) mSearchView.findViewById(search_button_id);
search_button_view.setOnTouchListener((view, motionEvent) -> {
        mSearchView.setIconified(false);
        return true;
});
Mullion answered 24/5, 2019 at 0:44 Comment(1)
Could you please add some text for clarification of the changes you had made in the mentioned snippet of code?Apnea

© 2022 - 2024 — McMap. All rights reserved.