Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Create custom SearchView Search action

In my application I am using the Action Bar with a custom SearchView in it. I want to use some custom code when the user hits the enter button in the search field. But I cant find a way to catch the moment the user hits the enter button in the SearchView.

Now I tried several ways to capture the search action but I cant seem to find it.

My button is defined like this:

<item
    android:id="@+id/action_bar_search"
    android:title="Search"
    android:icon="@drawable/ic_menu_search"
    android:showAsAction="always"
    android:actionViewClass="-package-.CustomSearchView"
    />

With this belongs the CustomSearchView class:

public class CustomSearchView extends SearchView implements OnClickListener, android.view.View.OnKeyListener{

    public CustomSearchView(Context context) {
        super(context);
        this.setOnSearchClickListener(this);
        this.setSubmitButtonEnabled(true);
            this.setOnClickListener(this);
        this.setOnKeyListener(this);
    }

    @Override
    public void onClick(View v) {

        Log.d("SEARCH", "Onclick! " + this.getQuery());
    }

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub

        Log.d("SEARCH", "Search onkey");
        return false;
    }

}

Here you can see that I`ve tried 2 approaches:

  1. Catch the onclick event from the submit button. That didnt work since it only fired when I clicked on the search icon to open the SearchView.
  2. Catch keystrokes within the SearchView. That didnt work either. Got no response from any key that I pressed there.

Does anyone have a way of making a custom Search action with this SearchView?

UPDATE:

As stated below by PJL: You dont need a custom dialog for overriding this behaviour. The normal SearchView widget will work just fine for this.

like image 407
Bart Avatar asked Aug 04 '11 09:08

Bart


People also ask

How do I customize my SearchView widget?

just do your own search view, it is very simple. you then can include this layout in your activity layout file. This is a simple layout which includes a "search icon" followed by EditText, followed by "clear icon". The clear icon is shown after user types some text.

How do I create a search bar on Android?

Add the Search View to the App Bar To add a SearchView widget to the app bar, create a file named res/menu/options_menu. xml in your project and add the following code to the file. This code defines how to create the search item, such as the icon to use and the title of the item.

How do I add text in SearchView?

You can use setQuery() to change the text in the textbox. However, setQuery() method triggers the focus state of a search view, so a keyboard will show on the screen after this method has been invoked. To fix this problem, just call searchView.


1 Answers

Get your search view and add an OnQueryTextListener

final SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
    @Override
    public boolean onQueryTextChange(String newText) {
        // Do something
        return true;
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        // Do something
        return true;
    }
};

searchView.setOnQueryTextListener(queryTextListener);

By the way my menu layout is as follows;

<item android:id="@+id/menu_search"
    android:showAsAction="ifRoom"
    android:actionViewClass="android.widget.SearchView" />
like image 52
PJL Avatar answered Sep 29 '22 14:09

PJL