Sunday, 1 September 2019

How to set [ENTER/SEARCH/ETC...] key trigger on AutocompleteSupportFragment

I have an AutocompleteSupportFragment for the Places API:

<fragment android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:id="@+id/auto_complete"
        android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
        android:outlineProvider="bounds"
        android:elevation="5dp"
        app:backgroundTint="@color/colorWhite"
        android:imeOptions="actionSearch"/>

I have initialized and set everything right but what I can't figure out is how to set a trigger on the keyboard's Search key. I tried this but the key is completely ignoring the code:

autocompleteSupportFragment.getView().setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int i, KeyEvent keyEvent) {
                if ((keyEvent.getAction() == KeyEvent.KEYCODE_SEARCH || keyEvent.getAction() == KeyEvent.KEYCODE_ENTER || i == EditorInfo.IME_ACTION_SEARCH || i == EditorInfo.IME_ACTION_DONE || keyEvent.getAction() == KeyEvent.ACTION_DOWN || keyEvent.getAction() == KeyEvent.KEYCODE_ENTER) && keyEvent.isTracking() && !keyEvent.isCanceled()) {
                    Toast.makeText(getApplicationContext(), "Could not predict", Toast.LENGTH_SHORT).show();
                    return true;
                }
                return false;
            }
        });

Is there something I haven't tried?



from How to set [ENTER/SEARCH/ETC...] key trigger on AutocompleteSupportFragment

No comments:

Post a Comment