5

I want to create a layout where the user types in the artist name and when he presses search on the virtual keyboard the list of artists are displayed.

View rootView = (View) inflater.inflate(R.layout.fragment_search, container, false);
EditText searchArtist = (EditText) rootView.findViewById(R.id.searchArtist);
searchArtist.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            makeToast();
            return true;
        }
        return false;
    }
});

Here is my xml

<EditText
    android:id="@+id/searchArtist"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="@string/search_artist"
    android:imeOptions="actionSearch"
    android:inputType="text">
    <requestFocus />

I have searched a lot in other stackoverflow post regarding this but none of the solutions seem to be working.

MinSDK 21, Testing on nexus 6, compileSdkVersion 22, buildToolsVersion '22.0.1'.

Also note that the searchArtist(edittext) is inside a fragment.

More context: the listener code is inside onCreateView method.

poortechworker
  • 627
  • 1
  • 6
  • 21

4 Answers4

2

I tested a similar code on Android 5.1.1. It works. I suggested to insert a log message to check if the onEditorAction is being called:

public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
     Log.v("TAG", "onEditorAction(): " + actionId);  
     if (actionId == EditorInfo.IME_ACTION_SEARCH) {
         Log.v("TAG", "onEditorAction(): entered");
     ...

This way, you can ensure the method is really being called. EditorInfo.IME_ACTION_SEARCH is constant: 3

I assume that makeToast(); is a method that you create, right?

Just to add more information, I tested your code in a Fragment. So, I really don't believe the problem is where your code was inserted. You just need to ensure that listener was set before you need to use it. Hope this can help you

public class MainFragment extends Fragment {
....
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

        // view is a reference to whole fragment layout (where searchArtist is inserted)
        View view = inflater.inflate(R.layout.fragment, null);

        // Find searchArtist (There's only one @id/searchArtist inside View)
        EditText searchArtist = (EditText)view.findViewById(R.id.searchArtist);

        searchArtist.setOnEditorActionListener(new  TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                Log.v("TEMP", "I'm being clicked " + actionId);
                return true;
            }
            return false;
        }
    });
    return view;
    }// End of onCreateView
} // End of Fragment

My xml:

<EditText android:id="@+id/searchArtist"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"    
     android:hint="@string/search_artist"    
     android:imeOptions="actionSearch"  
     android:inputType="text" />
guipivoto
  • 18,327
  • 9
  • 60
  • 75
  • Yes, makeToast() is a method which I used for pseudo Debugging. I tried adding the verbose log you recommended. The listener is not being called. Did you try this inside a fragment or activity? – poortechworker Jun 03 '15 at 00:38
  • I tested in a activity. But I have a very similar code in a fragment of my app... it works as well. Let's check more... – guipivoto Jun 03 '15 at 00:48
  • Yes, mine too. I have other listener methods which work perfectly fine, only this doesn't. For more context, I have this code in my onCreateView. Does that change anything? – poortechworker Jun 03 '15 at 01:00
  • 1
    I believe the place where you inserted the code is not the problem. I tested right now putting the code inside onCreateView and event is being reported normally... EditText searchArtist is not null. Otherwise, you app would crash when setting the editorAction listener. So, it was inflated... I'm not sure.. maybe, you are setting setOnEditorActionListener to the wrong object.. I'm not sure. Try to clean your project again .. This way, R.java will be recreated and all View IDs will be populated again. In Eclipse, you can clean your project by Project->Clean – guipivoto Jun 03 '15 at 01:10
  • Try to share also how you retrieve the reference to searchArtist (how you created that variable) – guipivoto Jun 03 '15 at 01:15
  • Added how I retrieve the reference of searchArtist. I did a clean build and tested it again. Tested in emulator as well (API 19). Still nothing. Let me know if you need more info to help me debug this. Thank you. – poortechworker Jun 03 '15 at 01:23
  • I posted a code which is working for me. In a fragment... Only diference is how the view was inflated.. – guipivoto Jun 03 '15 at 01:33
  • I tried writing exactly your way. The problem still persists. Can you explain this line "You just need to ensure that listener was set before you need to use it". I am new to android development. – poortechworker Jun 03 '15 at 01:43
  • I meant you should set the listener before user can click (and use)... But since you did that in onCreateView, your code is ok (you set the listener before editText become available to the user). – guipivoto Jun 03 '15 at 01:53
1

After trying all the solutions, including setOnKeyListener and textChangeListener. The final solution was deleting the project and starting from scratch. I guess there was an issue with the environment.

I really like Android Studio but sometimes these bugs set me back.

poortechworker
  • 627
  • 1
  • 6
  • 21
0

As suggested here, try listening to the key event on the editor action.

Docs can be found here

Community
  • 1
  • 1
Tim Mutton
  • 756
  • 8
  • 17
  • I already tried that. Unfortunately it does not work. Also, As soft input methods can use multiple and inventive ways of inputting text, there is no guarantee that any key press on a soft keyboard will generate a key event: this is left to the IME's discretion, and in fact sending such events is discouraged. You should never rely on receiving KeyEvents for any key on a soft input method. Src -> http://developer.android.com/reference/android/view/KeyEvent.html – poortechworker Jun 03 '15 at 00:30
0

I had the same problem: neither the OnEditorActionListener nor the onKeyListener worked.

But the TextChangeListener solution did the trick. For me.

snippet:

protected void onCreate(Bundle savedInstanceState){
  ...
  someField = (EditText) this.findViewById(R.id.some_field);
  someField.addTextChangeListener(someListener);
  ...
}
Stephan Richter
  • 1,139
  • 11
  • 31