M4rk
M4rk

Reputation: 2272

Android SearchView on ActionBar, detect click on Search

In my application, like many other, there is a common search widget (http://developer.android.com/guide/topics/search/search-dialog.html#UsingSearchWidget).

I would change fragment (and pass to it the term searched) only when the search button on the keyboard is pressed.

I've tried with

searchView.setOnSearchClickListener(new OnClickListener() { 
@Override public void onClick(View v) { 
} });

but it is triggered when you press the button ON THE ACTION BAR, that uncollapse the search input, and not when the keybord button search is clicked.

Do you know how is possible?

Upvotes: 11

Views: 18209

Answers (6)

AkashToSky
AkashToSky

Reputation: 115

 searchView.setOnSearchViewListener(new MaterialSearchView.SearchViewListener()
    {
        @Override
        public void onSearchViewShown()
        {
            //write your code
        }

        @Override
        public void onSearchViewClosed()
        {
            //write your code 
        }
    });

Hope this helps.

Upvotes: 0

Ndivhuwo
Ndivhuwo

Reputation: 288

For me I was getting the EditText from the SearchView, and capturing the "setOnEditorActionListener", but the default search action was still occurring, the Activity was refreshing after submit. To solve this I removed

searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

From my onCreateOptionsMenu method, And now all is working perfectly

Upvotes: 0

Rachit Agarwal
Rachit Agarwal

Reputation: 1

Firstly, set IME option as IME_ACTION_SEARCH for SearchView (Although keyboard might set it automatically)

searchView.setImeOptions(EditorInfo.IME_ACTION_SEARCH);

Next, to check whether search icon is pressed on keypad or not

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

          public boolean onQueryTextSubmit(String query) {
               searchView.clearFocus();     // Close keyboard on pressing IME_ACTION_SEARCH option
               Log.d(TAG, "QueryTextSubmit : "+ query);
               //load search query
               return true;
    }

          @Override
          public boolean onQueryTextChange(String newText) {
               Log.d(TAG, "QueryTextChange: "+ newText);
               return false;
          }
});

NOTE : Here, when Search icon on Keyboard is pressed onQueryTextSubmit() is called.

Moreover, serchView.clearFocus() collapses the keyboard.

Hopefully, this is what you are looking for.

Upvotes: 0

Jignesh Goyani
Jignesh Goyani

Reputation: 1020

Try This Code. It's very small and understand.

  @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu, menu);


        // Associate searchable configuration with the SearchView
        SearchManager searchManager =
                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView =
                (SearchView) menu.findItem(R.id.search).getActionView();
        final EditText searchEditText = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);

        searchEditText.setHint("Search");

        searchEditText.setHintTextColor(getResources().getColor(R.color.white));
        searchView.setSearchableInfo(
                searchManager.getSearchableInfo(getComponentName()));

        searchEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    //run query to the server
                    Log.e("onClick: ", "-- " + searchEditText.getText().toString().trim());

                }
                return false;
            }
        });


        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.

        return super.onOptionsItemSelected(item);
    }

menu.xml File

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/search"
        android:icon="@android:drawable/ic_menu_search"
        android:title="Search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="always" />

</menu>

Upvotes: 2

Timur Gilfanov
Timur Gilfanov

Reputation: 686

You can use this tо detect search key press in action bar search:

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

            @Override
            public boolean onQueryTextSubmit(String s) {
                [You actions here]
                return false;
            }
}

Upvotes: 19

Illegal Argument
Illegal Argument

Reputation: 10338

This is the code I used for my edittext. Similar thing could work for you:

editTextSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
    if (actionId == EditorInfo.IME_ACTION_SEARCH) {
      //run query to the server
    }
    return false;
}
});

Upvotes: 1

Related Questions