Jinhyung
Jinhyung

Reputation: 53

Android hiding keyboard for autocomplete

I am trying to hide keyboard when I select one of the autocomplete results. This is my code for the autocomplete.

input_address.setFocusable(false);
input_address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<Place.Field> fieldList = Arrays.asList(Place.Field.ADDRESS, Place.Field.LAT_LNG, Place.Field.NAME);
                Intent intent = new Autocomplete.IntentBuilder(AutocompleteActivityMode.OVERLAY, fieldList).build(MainActivity.this);
                startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE);
            }
        });

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == AUTOCOMPLETE_REQUEST_CODE && resultCode == RESULT_OK) {

            Place place = Autocomplete.getPlaceFromIntent(data);
            input_address.setText(place.getName());
            latitude = place.getLatLng().latitude;
            longitude = place.getLatLng().longitude;
            departure = "\"" + latitude + "\"" + ":" + "\"" + longitude + "\"";
        }
        if (resultCode == AutocompleteActivity.RESULT_ERROR) {
            Status status = Autocomplete.getStatusFromIntent(data);
            Toast.makeText(getApplicationContext(), status.getStatusMessage(),
                    Toast.LENGTH_SHORT).show();
        }

}

And this is my xml code for input_address

<EditText
        android:id="@+id/input_address"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_weight="1"
        android:hint="departure"
        android:textSize="20sp"
        android:inputType="text"
        android:imeOptions="actionDone"
        android:onClick="startAutocompleteActivity"
/>

How should I change my code to have keyboard when I click autocomplete result? I tried adding under OnActivityResult with IMM but it didn't work. Please Help Thank you.

Upvotes: 1

Views: 297

Answers (1)

Apil Pokharel
Apil Pokharel

Reputation: 71

input_address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<Place.Field> fieldList = Arrays.asList(Place.Field.ADDRESS, Place.Field.LAT_LNG, Place.Field.NAME);

//Code for hiding keyboard
 final InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);


                Intent intent = new Autocomplete.IntentBuilder(AutocompleteActivityMode.OVERLAY, fieldList).build(MainActivity.this);
                startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE);
            }
        });

Upvotes: 1

Related Questions