Android SearchView onclick

Christine Bauers picture Christine Bauers · Feb 13, 2013 · Viewed 25.7k times · Source

I have a searchView which looks like this:

private void setupSearchView() {
    mSearchView = (SearchView) getActivity().findViewById(
            R.id.search_view_neue);
    setSearchViewBackground();
    mSearchView.setOnClickListener(this);
    mSearchView.setOnQueryTextListener(this);
}




    public boolean onQueryTextSubmit(String query) {
    searchcounter = searchcounter + 1;

    setSearchViewBackground();
    ArrayList<WissensdokumenteRecord> documents = settingListContent(new ArrayList<WissensdokumenteRecord>());

    setListAdapter(new NeueWissensdokumentItemAdapter(
            inflater.getContext(), R.layout.row_example, documents));
    InputMethodManager im = (InputMethodManager) getActivity()
            .getSystemService(Context.INPUT_METHOD_SERVICE);
    im.hideSoftInputFromWindow(getActivity().getCurrentFocus()
            .getWindowToken(), 0);

    return false;
}




<SearchView
    android:id="@+id/search_view_neue"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_marginLeft="40dp"
    android:layout_marginRight="40dp"
    android:background="@drawable/border_searchview"
    android:maxWidth="540dp"
    android:queryHint="Neue Dokumente suchen" >
</SearchView>

So, the behaviour of the search view is, that the keyboard opens by clicking the search button. Now I can search for something. Is it possible to do a search by clicking anywhere in the searchview? Does anyone has an example for me?

Answer

Spiri picture Spiri · Jul 24, 2014

I managed to do this in the following way:

Setup the search view on click listener

mSearchView.setOnClickListener(this);

Catch the onClick event:

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.searchView:
            mSearchView.onActionViewExpanded();
            break;
    }
}

In this way the keyboard and search are activated if you click antwhere on the search bar.