4

I added an AutocompleteSupportFragment in an AlertDialog.
AlertDialog
I want to make sure location field is not empty after user clicks "Create" button. (Just like how I make sure other fields are not empty.)
Fields validation
I wish there is something like a setOnPlaceUnselectedListener. But AutocompleteSupportFragment only has setOnPlaceSelectedListener, which is not ideal because it won't know if user enters some location and then clears the input.

AutocompleteSupportFragment does have a setText method to set the text to display in the search input field. But it seems it doesn't have a corresponding getText method.

Is there anyway to validate whether AutocompleteSupportFragment has a Place selected or is empty? .xml

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

Activity.java


autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
            @Override
            public void onPlaceSelected(Place place) {
                // Updating my class variables here doesn't help. 
                // If user first selects a place, then clears it and click "Create", 
                // I won't know that the location field is empty.
                Log.i(TAG, "Place: " + place.getName() + ", " + place.getId());
            }

            @Override
            public void onError(Status status) {
                Log.i(TAG, "An error occurred: " + status);
            }
        });
SomeGuy
  • 43
  • 3

2 Answers2

1

Your understanding is correct in that there is currently no "setOnPlaceUnselectedListener" or "getText" or any other method through the Places SDK that can help you validate whether the input is empty.

However, there's a related feature request for exposing a onClearListener() in Google's Issue Tracker which I suggest starring to increase visibility and subscribe to notifications:

https://issuetracker.google.com/issues/35828573

Furthermore, there's a "hack" or workaround mentioned in one of the comments to get the clear button's View. Note that for AutocompleteSupportFragment the button's id is places_autocomplete_clear_button.

So for example, you can create a class variable:

private String selectedPlace = null;

and use it as a flag for when a place has been selected or unselected.

    autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {

        @Override
        public void onPlaceSelected(Place place) {
            selectedPlace = place.getName();
        }

        @Override
        public void onError(Status status) {
            selectedPlace = null;
        }

    });

    View clearButton = autocompleteFragment.getView().findViewById(R.id.places_autocomplete_clear_button);
    clearButton.setOnClickListener(view -> {
        autocompleteFragment.setText("");
        selectedPlace = null;
    });

    final Button button = findViewById(R.id.button_id);
    button.setOnClickListener(v -> {
        if (selectedPlace == null) {
            Toast.makeText(getApplicationContext(), "Location cannot be empty", Toast.LENGTH_LONG).show();
        }
    });

I have tested this on my end so I can confirm it works and I hope it helps you too!

evan
  • 5,443
  • 2
  • 11
  • 20
  • The `onClearListener()` is exactly what I need! I've stared the issue - can't believe it's marked as just p4. And thank you so much for the workaround - it definitely works! – SomeGuy Dec 08 '19 at 15:51
  • Glad to hear & happy to help! :) – evan Dec 08 '19 at 16:46
  • I'm getting this error when trying your suggested hack. `Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference` – Rami Alloush Apr 12 '20 at 04:00
  • Can you post your (reproducible) code please? Preferably in a new question. I'll take a look. – evan Apr 12 '20 at 08:16
0

I've already answered this here. You can find the clear button inside the fragment and listen to it.

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);
    }
});

After that, you can just check if homeAddressPlaceID is empty or not.

Rami Alloush
  • 2,308
  • 2
  • 27
  • 33