Android: How to detect click on custom SearchView element
Asked Answered
T

5

33

I m creating a custom SearchView, but so far I didn't find a (click)event or listener, if the user clicks on the searchView widget. So SearchView is close, user clicks on the search Icon (which listener?!), SearchView is opened. See the picture below. I need that event, because I want to change the UI after user opens the searchView

enter image description here

EDIT: What I tried so far is

  • setOnClickListener to SearchView Element -> but it's only triggered if the SearchView is open(2nd picture)

  • searched SearchView Api, but there isn't a listener for that action (or am I blind?)

  • tried a workaround with onWindowFocusChanged of the searchView, but due the fact that the searchView is hidden in some kind of youtube/facebook sidebar, this doesn't work

Any ideas? I attached my SearchView Code as well, but don't think the code is wrong. just no listener yet. btw: any workarounds are welcome. But it would be nice, if the SearchView is closed at the beginning! (like in the pictures above)

public class AmplifySearchView extends SearchView implements
        OnQueryTextListener, OnFocusChangeListener,
        OnCloseListener {

    private static final String DEBUG_TAG = "AmplifySeachView";

    private Context context;
    private DataBaseController datasource;
    private StationListView searchResultListView;
    private TextView noResultTextView;

    private Boolean searchStarted = false;

    public AmplifySearchView(Context context) {
        super(context);
        this.context = context;
    }

    public AmplifySearchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public void initSearchView(StationListView view, TextView noResultTextView) {
        this.searchResultListView = view;
        this.searchResultListView.setActivityId(Constants.UI_SEARCH_RESULT_LIST);
        this.noResultTextView = noResultTextView;


        this.setOnQueryTextListener(this);
        this.setOnFocusChangeListener(this);
        this.setOnCloseListener(this);
        this.setOnCloseListener(this);

        setIconifiedByDefault(true);

        datasource = DataBaseController.getInstance(context);

        int id = this.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
        TextView textView = (TextView) findViewById(id);
        textView.setTextColor(Color.WHITE);


        resetSearchInputTextField();
    }

    private void resetSearchInputTextField() {
        setQueryHint(getResources().getString(R.string.search_hint));
    }

    @Override
    public boolean onClose() {
        Log.d(DEBUG_TAG, "onClose()");
        searchResultListView.setVisibility(View.GONE);
        noResultTextView.setVisibility(View.GONE);

        searchStarted = false;

        Intent intent = new Intent(Constants.SEARCH_ENDED);
        intent.addCategory(Constants.UI_AMPLIFY_CATEGORY);
        context.sendBroadcast(intent);

        return false;
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        Log.d(DEBUG_TAG,"onFocusChange()" + hasFocus);

    }


    @Override
    public boolean onQueryTextChange(String newText) {

        //do sth...
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        Log.d(DEBUG_TAG, "onQueryTextSubmit -> " + query);
        return true;
    }

    @Override
    protected void onFocusChanged(boolean gainFocus, int direction,
            Rect previouslyFocusedRect) {
        Log.d(DEBUG_TAG, "onQueryTextSubmit -> " + gainFocus);
        // TODO Auto-generated method stub
        super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
    }

    @Override
    public void onActionViewCollapsed() {
        Log.d(DEBUG_TAG, "onActionViewCollapsed");
        super.onActionViewCollapsed();
    }

    @Override
    public void onActionViewExpanded() {
        Log.d(DEBUG_TAG, "onActionViewExpanded");
        super.onActionViewExpanded();
    }
}

btw: on Focus change don't work here, because searchview is invisible at the beginning and insert in the ui later...

Transportation answered 30/1, 2013 at 11:23 Comment(3)
the second onFocusChanged isn't called either ?Explicit
no, i just checked again. actually, none of the focus changed listeners are called. not even after appstart (which just confuses me mote, beacause i saw them before...)Transportation
A really hacky work around might be to listen for a click in the area of the searchview? You should be able to pull the location and dimensions of it on the view, regardless of if it's dynamically placed or not. That said the answer below about setOnSearchClickListener seems like a much better solution if it actually works for you.Amylopsin
E
60

How about this?

public void setOnSearchClickListener (View.OnClickListener listener)

Description from android developer website says.. Sets a listener to inform when the search button is pressed. This is only relevant when the text field is not visible by default. Calling setIconified(false) can also cause this listener to be informed.

http://developer.android.com/reference/android/widget/SearchView.html#setOnSearchClickListener(android.view.View.OnClickListener)

Epicure answered 11/2, 2013 at 1:45 Comment(1)
thanks! this solved my problem. i used this in a wrong way at the beginning. how to use it, look at post below!Transportation
R
9

for text field visible use:

SearchView searchView = (SearchView) findViewById(R.id.searchView);
    searchView.setOnSearchClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //your code here
        }
    });

for text field invisible use:

searchView.setOnCloseListener(new SearchView.OnCloseListener() {
        @Override
        public boolean onClose() {
            //your code here
            return false;
        }
    });
Ruching answered 8/3, 2015 at 9:43 Comment(0)
B
9

Try this in Kotlin:

    searchView.setOnQueryTextFocusChangeListener { _ , hasFocus ->
        if (hasFocus) {
            // searchView expanded
        } else {
            // searchView not expanded
        }
    }

In Java :

searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            // searchView expanded
        } else {
            // searchView not expanded
        }
    }
});
Borer answered 26/12, 2017 at 13:42 Comment(0)
T
8
 public void setOnSearchClickListener (View.OnClickListener listener)

solves the issue. actually i thought i ve been using this listener before, but the OnSearchClickListener isn't the OnClickListener in my SearchView. You have to declare that listener from the outside like

 AmplifySearchView sv = (AmplifySearchView) findViewById(...);

 sv.setOnSearchClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.d("CLICK","CLICK");

        }
 });
Transportation answered 11/2, 2013 at 10:8 Comment(0)
A
2

This was written with API 15. It has not been tested. Details at bottom.

public class MySearchView extends SearchView {
    private boolean mHasFocus;
    private OnOpenListener mOnOpenListener;

    public MySearchView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setOnOpenListener(OnOpenListener onOpenListener) {
        mOnOpenListener = onOpenListener;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        boolean onTouchEvent = super.onTouchEvent(event);

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            if (!mHasFocus) {
                mHasFocus = true;

                if (mOnOpenListener != null) {
                    mOnOpenListener.onOpen();
                }
            }

            break;
        }

        return onTouchEvent;
    }

    @Override
    protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
        super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);

        if (!gainFocus) {
            mHasFocus = false;
        }
    }

    public interface OnOpenListener {
        public void onOpen();
    }
}

You give it an OnOpenListener and when the View is clicked it will fire the event. When it loses focus it will reset the boolean value. I think it will also fire the OnClose aswell that is already built in. What can be a problem is the focus bit; I have had problems before using a SearchView and it will not give up it's focus. Maybe you can outside of the class assign when the View shall have focus or not. If you want more features you have to build your own.

Ambo answered 4/2, 2013 at 12:28 Comment(1)
thanks for your reply, doesn't work for me. i got the idea but still it doesn't inform me, if the searchView icon is clicked. i ll look for alternatives soon!Transportation

© 2022 - 2024 — McMap. All rights reserved.