Android SearchView on ActionBar, detect click on Search
Asked Answered
S

6

11

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?

Stomatology answered 20/6, 2014 at 18:39 Comment(0)
T
19

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;
            }
}
Teniers answered 11/8, 2014 at 12:43 Comment(0)
G
2

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>
Gerrilee answered 13/5, 2016 at 10:30 Comment(0)
T
1

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;
}
});
Tedder answered 20/6, 2014 at 18:42 Comment(0)
E
0

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.

Esoterica answered 2/10, 2016 at 22:11 Comment(0)
T
0

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

Tadich answered 21/10, 2018 at 17:32 Comment(0)
S
0
 searchView.setOnSearchViewListener(new MaterialSearchView.SearchViewListener()
    {
        @Override
        public void onSearchViewShown()
        {
            //write your code
        }

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

Hope this helps.

Stoup answered 4/1, 2019 at 7:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.