Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SearchView onclick

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?

like image 476
Christine Bauers Avatar asked Feb 13 '13 10:02

Christine Bauers


People also ask

What is SearchView in android?

android.widget.SearchView. A widget that provides a user interface for the user to enter a search query and submit a request to a search provider. Shows a list of query suggestions or results, if available, and allows the user to pick a suggestion or result to launch into.

How do I get text from SearchView?

To get text of SearchView , use searchView. getQuery() .


2 Answers

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.

like image 60
Spiri Avatar answered Oct 11 '22 10:10

Spiri


This will do exactly what you're trying to achieve. setIconified(false) will keep the cross icon at this end of the SearchView so that the user can still cancel the same way as if the magnifying glass was clicked.

searchView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        searchView.setIconified(false);
    }
});
like image 37
anni Avatar answered Oct 11 '22 10:10

anni