0

I am trying to inflate an AutoCompleteSupportFragment which uses Google Places API, in an onClick event of an editText.

I want to inflate that fragment when the editText is clicked, so that the user might enter a location, select from the suggestions in the auto complete fragment and the location name will be shown on the editText.

I checked this link here, but it didnt work for me.

places_auto_complete_fragment.xml

    <?xml version="1.0" encoding="utf-8"?>
    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/place_autocomplete_fragment"
    android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment" />

how I am trying to inflate it

sourceEditText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            LayoutInflater layoutInflater = getLayoutInflater();
            layoutInflater.inflate(R.layout.places_auto_complete_fragment,addTripLinearLayout,false);
        }
    });

Any help is appreciated. Thanks

arnavJJ
  • 137
  • 1
  • 10

2 Answers2

1
  1. AutocompleteSupportFragment presents a search box button to the user, which presents a search box UI when clicked
  2. Inflating the layout loads the UI but you have to add it to your view in order to make it visible.
1

Use Intent builder instead fragment in editText....

initialise on Oncreate

   // Initialize Places.
   Places.initialize(getApplicationContext(), "***YOUR API KEY***");

   // Create a new Places client instance.
   PlacesClient placesClient = Places.createClient(this);

Then use below code on edittext on click event...

List<Place.Field> fields = Arrays.asList(Place.Field.ID, Place.Field.NAME);
// Start the autocomplete intent.
Intent intent = new Autocomplete.IntentBuilder(AutocompleteActivityMode.FULLSCREEN, fields).build(this);
startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            Place place = Autocomplete.getPlaceFromIntent(data);
            Log.i(TAG, "Place: " + place.getName() + ", " + place.getId());
        } else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
            // TODO: Handle the error.
            Status status = Autocomplete.getStatusFromIntent(data);
            Log.i(TAG, status.getStatusMessage());
        } else if (resultCode == RESULT_CANCELED) {
            // The user canceled the operation.
        }
    }
}

Please refer the below link https://stackoverflow.com/a/55045772/10579969...

if you are using only fragment please refer below code

       AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment)
                getSupportFragmentManager().findFragmentById(R.id.autocomplete_fragment);
    autocompleteFragment.setCountry("IN");    //country type
    autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME)); 
   //to indicate the types of place data that you want to get.
Navin Kumar
  • 3,393
  • 3
  • 21
  • 46