Stephane Maarek
Stephane Maarek

Reputation: 5352

SearchView to perform search inside same activity

I have a searchView in my SearchActivity and it creates an intent to launch a SearchActivityResults according to the documentation here: http://developer.android.com/training/search/setup.html

How do I need to go if I want my SearchView to trigger a search from within my activity, when the soft keyboard lower right button is pressed to trigger the search?

Upvotes: 8

Views: 1584

Answers (1)

Ramy Sabry
Ramy Sabry

Reputation: 368

you just need to get your searchView menu , use override onCreateOptionsMenu

SearchView searchView = (SearchView) MenuItemCompat
            .getActionView(mSearchMenuItem);
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {


        @Override
        public boolean onQueryTextSubmit(String s) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String s) {
            // do your search on change or save the last string in search
            return false;
        }
    });

    // you can get query  
    searchView.getQuery();

Upvotes: 15

Related Questions