8

I am using Google Places API for Android - PlaceAutocompleteFragment API in my project to search locations, when user selects a location then get the records using location lat-long and display it to ListView.

Now the problem is whenever user taps on clear button (X) of PlaceAutocompleteFragment I want clear ListView items for which I need to listen to clear button onClick? How to do that?

enter image description here
Any help will be appreciated.

autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
    @Override
    public void onPlaceSelected(Place place) {
        LatLng latLng=place.getLatLng();
        //hit web service, response parsing and add it to listview.
    }

    @Override
    public void onError(Status status) {
        Log.i(TAG, "An error occurred: " + status);
    }
});
Bharatesh
  • 8,943
  • 3
  • 38
  • 67
  • try removing that item from arraylist you setting to adapter and then call notifyDataSetChanged(). For more Please post code – Jagjit Singh Mar 21 '16 at 12:01
  • @JagjitSingh I know how to clear listview items.. brefore that I need to listen to close button click of PlaceAutocompleteFragment ... I don't find any method for this api. – Bharatesh Mar 21 '16 at 12:04
  • Post whole code for that. – Jagjit Singh Mar 21 '16 at 12:06
  • you dont have object of the placeautocomplete . you just fire an intent and thats it. the control goes into that fragment. so you can not have any listener for this. remember you have a onactivityresult for this. in my opnion there is no method for this. – Sagar Nayak Mar 21 '16 at 12:08
  • @JagjitSingh added more code.. I don't think HttpRequest, Response parsing code needed as its working fine.. The only main thing is set up onclick listener for clearn/close button of PlaceAutocompleteFragment. – Bharatesh Mar 21 '16 at 12:12
  • @SagarNayak thats true, I understand that but isn't it obvious that whenever we click clear, we need clear items of that result also. :) – Bharatesh Mar 21 '16 at 12:16
  • places api does not provide you with such method. are you using autocomplete or the fragment provided by google .? – Sagar Nayak Mar 21 '16 at 12:19
  • what yes ? autocomplete or google fragment ? – Sagar Nayak Mar 21 '16 at 12:24
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/106909/discussion-between-bharat-and-sagar-nayak). – Bharatesh Mar 21 '16 at 12:25
  • @SagarNayak autocomplete.. – Bharatesh Mar 21 '16 at 12:27
  • @SagarNayak got a solution.. check my answer... no need to setup manual location search.. – Bharatesh Mar 22 '16 at 05:14

3 Answers3

25

Looking at the source code of PlaceAutocompleteFragment I found those view ids and setup a onClickListener like this:

autocompleteFragment.getView().findViewById(R.id.place_autocomplete_clear_button)
    .setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // example : way to access view from PlaceAutoCompleteFragment
            // ((EditText) autocompleteFragment.getView()
            // .findViewById(R.id.place_autocomplete_search_input)).setText(""); 
            autocompleteFragment.setText("");
            view.setVisibility(View.GONE);
        }
});
Bharatesh
  • 8,943
  • 3
  • 38
  • 67
  • Hi ,can we get the access of edittext of searchview used in PlaceAutocompleteFragment if we call it using IntentBuilder – Yyy Sep 14 '16 at 05:45
  • 1
    @NancyY using this `id` you can get searchview edittext `R.id.place_autocomplete_search_input` – Bharatesh Sep 16 '16 at 04:07
  • Why don't you just autocompleteFragment.setText(""); – dvdciri Jan 05 '17 at 13:49
  • @dvdciri because you dont have a method setText for AutoCompleteFragment. – Bharatesh Jan 06 '17 at 05:12
  • 1
    It does have it, look at the documentation https://developers.google.com/android/reference/com/google/android/gms/location/places/ui/PlaceAutocompleteFragment – dvdciri Jan 06 '17 at 09:28
  • 1
    Great detective work and answer. Wish I could upvote more than once. – Tim Biegeleisen Aug 22 '17 at 16:39
  • @Bharatesh How did you look at the source code? I don't seem able to find it. – Mox Apr 03 '18 at 06:16
  • @Bharatesh can you write google's default codes for search input onClick like in the answer? – ninbit Sep 04 '18 at 22:09
  • @ninbit do you mean search input (edittext) listener? – Bharatesh Sep 05 '18 at 09:14
  • @Mox can you check through sdk/library files or open the file within studio. – Bharatesh Sep 05 '18 at 09:17
  • @Bharatesh yes. I want to add a listener for that edittext (R.id.place_autocomplete_search_input). If I add only with my needings, then the box is not working – ninbit Sep 05 '18 at 11:33
  • @ninbit if doubt default behavior(getting search result) will work if you override edittext listener as you provided your own listeners. Can you check what android is doing when edittext changes. Check the source code you might find something. – Bharatesh Sep 05 '18 at 11:54
  • @Bharatesh as you explain the problem is this: when I override the listener, the autocomplete just not working, even not triggered for typing. Is it possible to call the default listener with a method like 'super' keyword? – ninbit Sep 05 '18 at 12:32
  • @ninbit I doubt – Bharatesh Sep 05 '18 at 12:51
  • does anyone have this done in Kotlin? im having trouble with the `findViewById` requiring a Type inference for `R.id.place_autocomplete_clear_button` – Barcode Mar 01 '19 at 20:09
  • Hi all, In latest version, the id of the view is changed to "R.id.places_autocomplete_clear_button", kindly change it. – Sackurise Jun 11 '19 at 06:06
  • For those who can't find `R.id.places_autocomplete_clear_button`, instead try using: `com.google.android.libraries.places.R.id.places_autocomplete_search_input` – darkrider1287 Nov 17 '19 at 04:13
  • 2
    change places_autocomplete_clear_button instead of place_autocomplete_clear_button. For me the "s" was the problem – Terranology Nov 26 '19 at 16:42
0

The answer above did not solve my problem. I could solve it with a trick :)

    autocompleteFragment.getView().findViewById(R.id.place_autocomplete_clear_button).setPadding(0,0,0,55555);
0

I create the findViewById in the onCreate of the activity, I was getting the following error

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference

Because the fragment is not initialized/visible yet. I moved the listener inside the setOnPlaceSelectedListener and it's working fine now.

autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
    @Override
    public void onPlaceSelected(Place place) {
        homeAddressPlaceID = place.getId();
        Log.i(TAG, "Place: " + place.getName() + ", " + place);

        autocompleteFragment.getView().findViewById(R.id.places_autocomplete_clear_button)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        autocompleteFragment.setText("");
                        homeAddressPlaceID = "";
                    }
                });
    }

    @Override
    public void onError(Status status) {
        Log.i(TAG, "An error occurred: " + status);
    }
});
Rami Alloush
  • 2,308
  • 2
  • 27
  • 33