Barry Fruitman
Barry Fruitman

Reputation: 12656

How do i know when the user tapped the search key?

I've implemented a keyword search in my app but I don't get any callbacks when I tap the search (i.e. question mark) key on the soft keyboard. I've added a TextWatcher to my EditText but it isn't called when I tap search. Activity.onKeyUp() isn't called either.

Is there any way to know when the user taps the search key on the soft keyboard?

Thanks in advance...

Upvotes: 0

Views: 113

Answers (1)

Charaf Eddine Mechalikh
Charaf Eddine Mechalikh

Reputation: 1248

use searchView like this

 SearchView sv=(SearchView)findViewByid(R.id.sv);
      sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String s) {
                    return false;
                     //here is when clicking the search button in keyboard
                }

                @Override
                public boolean onQueryTextChange(String s) {
                   //here is when the text change
                    return false;
                }
            });

Edit i've found away to do that :

in the xml

  <EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/editText"  
    android:imeOptions="actionSearch"
    android:inputType="text"/>

in the onCreate methode

 ((EditText)findViewById(R.id.editText)).setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                Toast.makeText(MainActivity.this,"searching",Toast.LENGTH_LONG).show();
                return true;
            }
            return false;
        }
    });

that's all

Upvotes: 1

Related Questions