Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

android

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?

like image 490
Dev Avatar asked Oct 16 '22 12:10

Dev


1 Answers

"focus mode" = where we actually can type the address.

When we using the embedded fragment, it means we use default placeholder to trigger the "focus mode". If you want to trigger the "focus mode" from custom placeholder then you need to exclude the fragment from the layout.

a. Using embedded fragment / default placeholder

//GRADLE
implementation 'com.google.android.libraries.places:places:2.0.0'
remove the implementation 'com.google.android.gms:play-services-places:x.y' 

//XML LAYOUT
<fragment
    android:id="@+id/autocomplete_fragment"
    android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

// ACTIVITY
onCreate {
    var autocompleteFragment: AutocompleteSupportFragment? = null
    if (!Places.isInitialized()) {
        Places.initialize(applicationContext, "API KEY");
    }

    // Bind the fragment in layout xml
    autocompleteFragment =
        supportFragmentManager.findFragmentById(R.id.autocomplete_fragment) as? AutocompleteSupportFragment

    // Specify the types of place data to return.
    autocompleteFragment?.setPlaceFields(Arrays.asList(Place.Field.LAT_LNG))

    // Set up a PlaceSelectionListener to handle the response.
    autocompleteFragment?.setOnPlaceSelectedListener(object : PlaceSelectionListener {
        override fun onPlaceSelected(place: Place) {
            val str = "lat: ${place.latLng?.latitude}, long: ${place.latLng?.longitude}"
            Toast.makeText(mAct, str, Toast.LENGTH_SHORT).show()

        }

        override fun onError(status: Status) {
            Toast.makeText(mAct, "ERROR", Toast.LENGTH_SHORT).show()
        }

    })
}

b. Using custom placeholder

//GRADLE
implementation 'com.google.android.libraries.places:places:2.0.0'
remove the implementation 'com.google.android.gms:play-services-places:x.y'

// XML LAYOUT
    <Button
        android:layout_width="wrap_content"
        android:id="@+id/home_BTN_autocomplete"
        android:text="show autocomplete widget"
        android:layout_height="wrap_content" />

// ACTIVITY
val AUTOCOMPLETE_REQUEST_CODE = 1212
var mAct: Context? = null
onCreate {
    mAct = this // Needed for onactivityresult hook
    if (!Places.isInitialized()) {
        Places.initialize(applicationContext, "API-KEY");
    }
    home_BTN_autocomplete.setOnClickListener {
        val fields = Arrays.asList(Place.Field.LAT_LNG)
        val intent = Autocomplete.IntentBuilder(AutocompleteActivityMode.FULLSCREEN, fields).build(this)
        startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE)
    }
}

 onActivityResult {
    if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
        if (resultCode == Activity.RESULT_OK && data != null) {
            val place = Autocomplete.getPlaceFromIntent(data)
            val str = "lat: ${place.latLng?.latitude}, long: ${place.latLng?.longitude}"
            Toast.makeText(mAct, str, Toast.LENGTH_SHORT).show()

        } else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
        } else if (resultCode == Activity.RESULT_CANCELED) {
        }
    }
}

like image 123
nfl-x Avatar answered Oct 19 '22 00:10

nfl-x