Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setOnEditorActionListener is not called in lollipop

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.

like image 506
Gaurav Pandey Avatar asked Oct 19 '22 10:10

Gaurav Pandey


1 Answers

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" />
like image 173
W0rmH0le Avatar answered Oct 22 '22 01:10

W0rmH0le