Android: How to detect click on custom SearchView element

longi picture longi · Jan 30, 2013 · Viewed 45.2k times · Source

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...

Answer

chuthan20 picture chuthan20 · Feb 11, 2013

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)