AutocompleteSupportFragment

public class AutocompleteSupportFragment extends Fragment


Implements a fragment that presents a search box button to the user, which presents a search box UI when clicked. As the user types, the autocomplete service returns Place predictions for places such as businesses, addresses and points of interest. When the user selects a place, the autocomplete service returns the response via the PlaceSelectionListener.

Summary

Public constructors

Public methods

static AutocompleteSupportFragment

Create a new autocomplete fragment with default config.

void
onActivityResult(int requestCode, int resultCode, @Nullable Intent data)

This method is deprecated.

void
onCreate(@Nullable Bundle savedInstanceState)
void
void
void
onViewCreated(View view, @Nullable Bundle savedInstanceState)
AutocompleteSupportFragment

Sets the type of activity to open when a user initiates a query.

AutocompleteSupportFragment
setCountries(String[] countries)

See setCountries.

AutocompleteSupportFragment
setCountries(List<String> countries)

Sets countries to restrict results to.

AutocompleteSupportFragment

This method is deprecated.

Use setCountries instead.

AutocompleteSupportFragment

Sets the hint text to display in the search input field when there is no text entered.

AutocompleteSupportFragment

Biases the autocomplete results to a particular area.

AutocompleteSupportFragment
setLocationRestriction(
    @Nullable LocationRestriction locationRestriction
)

Restricts the autocomplete results to a particular area.

AutocompleteSupportFragment

Sets a listener that will be notified when a place is selected.

AutocompleteSupportFragment

Sets the desired fields of the Place object returned from selecting an autocomplete result.

AutocompleteSupportFragment

Sets the unicode country/region code (CLDR) of the location where the request is coming from.

AutocompleteSupportFragment

Sets the text to display in the search input field.

AutocompleteSupportFragment

This method is deprecated.

Use setTypesFilter.

AutocompleteSupportFragment
setTypesFilter(List<String> placeTypes)

Sets the filter that restricts the type(s) of results returned from the response.

Extension functions

final @NonNull Flow<@NonNull PlaceSelectionResult>

Returns a Flow of PlaceSelectionResults that are emitted when a place is selected or an error occurs.

Inherited methods

From androidx.fragment.app.Fragment
void
dump(
    @NonNull String prefix,
    @Nullable FileDescriptor fd,
    @NonNull PrintWriter writer,
    @Nullable String[] args
)
final boolean
final @Nullable FragmentActivity
boolean
boolean
final @Nullable Bundle
final @NonNull FragmentManager
@Nullable Context
@NonNull CreationExtras
@NonNull ViewModelProvider.Factory
Object
Object
final @Nullable FragmentManager

This method is deprecated.

final Object
final int
final @NonNull LayoutInflater
@NonNull Lifecycle
@NonNull LoaderManager

This method is deprecated.

final @Nullable Fragment
final @NonNull FragmentManager
Object
final @NonNull Resources
final boolean

This method is deprecated.

Object
final @NonNull SavedStateRegistry
Object
Object
final @NonNull String
getString(int resId)
final @Nullable String
final @Nullable Fragment

This method is deprecated.

final int

This method is deprecated.

final @NonNull CharSequence
getText(int resId)
boolean

This method is deprecated.

@Nullable View
@NonNull LifecycleOwner
@NonNull LiveData<LifecycleOwner>
@NonNull ViewModelStore
final boolean
final int
static @NonNull Fragment

This method is deprecated.

final boolean
final boolean
final boolean
final boolean
final boolean
final boolean
final boolean
final boolean
final boolean
void
onActivityCreated(@Nullable Bundle savedInstanceState)

This method is deprecated.

void
void

This method is deprecated.

void
boolean
@Nullable Animation
onCreateAnimation(int transit, boolean enter, int nextAnim)
@Nullable Animator
onCreateAnimator(int transit, boolean enter, int nextAnim)
void
void

This method is deprecated.

@Nullable View
onCreateView(
    @NonNull LayoutInflater inflater,
    @Nullable ViewGroup container,
    @Nullable Bundle savedInstanceState
)
void
void

This method is deprecated.

void
void
@NonNull LayoutInflater
onGetLayoutInflater(@Nullable Bundle savedInstanceState)
void
onHiddenChanged(boolean hidden)
void
onInflate(
    @NonNull Context context,
    @NonNull AttributeSet attrs,
    @Nullable Bundle savedInstanceState
)
void

This method is deprecated.

void
onMultiWindowModeChanged(boolean isInMultiWindowMode)
boolean

This method is deprecated.

void

This method is deprecated.

void
void
onPictureInPictureModeChanged(boolean isInPictureInPictureMode)
void

This method is deprecated.

void
onPrimaryNavigationFragmentChanged(
    boolean isPrimaryNavigationFragment
)
void
onRequestPermissionsResult(
    int requestCode,
    @NonNull String[] permissions,
    @NonNull int[] grantResults
)

This method is deprecated.

void
void
void
onViewStateRestored(@Nullable Bundle savedInstanceState)
void
final @NonNull ActivityResultLauncher<I>
<I, O> registerForActivityResult(
    @NonNull ActivityResultContract<I, O> contract,
    @NonNull ActivityResultCallback<O> callback
)
void
final void
requestPermissions(@NonNull String[] permissions, int requestCode)

This method is deprecated.

final @NonNull FragmentActivity
final @NonNull Bundle
final @NonNull Context
final @NonNull FragmentManager

This method is deprecated.

final @NonNull Object
final @NonNull Fragment
final @NonNull View
void
void
void
void
void
void
void
void
setHasOptionsMenu(boolean hasMenu)

This method is deprecated.

void
void
setMenuVisibility(boolean menuVisible)
void
void
setRetainInstance(boolean retain)

This method is deprecated.

void
void
void
void
setTargetFragment(@Nullable Fragment fragment, int requestCode)

This method is deprecated.

void
setUserVisibleHint(boolean isVisibleToUser)

This method is deprecated.

boolean
void
void
startActivityForResult(@NonNull Intent intent, int requestCode)

This method is deprecated.

void
startIntentSenderForResult(
    @NonNull IntentSender intent,
    int requestCode,
    @Nullable Intent fillInIntent,
    int flagsMask,
    int flagsValues,
    int extraFlags,
    @Nullable Bundle options
)

This method is deprecated.

void
@NonNull String
void

Public constructors

AutocompleteSupportFragment

public AutocompleteSupportFragment()

Public methods

newInstance

public static AutocompleteSupportFragment newInstance()

Create a new autocomplete fragment with default config.

onActivityResult

public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data)

onCreate

public void onCreate(@Nullable Bundle savedInstanceState)

onResume

public void onResume()

onSaveInstanceState

public void onSaveInstanceState(Bundle bundle)

onViewCreated

public void onViewCreated(View view, @Nullable Bundle savedInstanceState)

setActivityMode

public AutocompleteSupportFragment setActivityMode(AutocompleteActivityMode mode)

Sets the type of activity to open when a user initiates a query.

setCountries

public AutocompleteSupportFragment setCountries(String[] countries)

See setCountries.

setCountries

public AutocompleteSupportFragment setCountries(List<String> countries)

Sets countries to restrict results to.

This must be a list of ISO 3166-1 Alpha-2 country codes (case insensitive). If no countries are set, no country filtering will take place.

This will override values set with setCountry.

Up to five countries are supported. See the components parameter here.

setCountry

public AutocompleteSupportFragment setCountry(@Nullable String country)

See setCountries for details.

This will override countries set with setCountries.

setHint

public AutocompleteSupportFragment setHint(@Nullable CharSequence hint)

Sets the hint text to display in the search input field when there is no text entered.

If the hint is unset or explicitly set to null, the hint text will be set to the default value.

setLocationBias

public AutocompleteSupportFragment setLocationBias(@Nullable LocationBias locationBias)

Biases the autocomplete results to a particular area.

setLocationRestriction

public AutocompleteSupportFragment setLocationRestriction(
    @Nullable LocationRestriction locationRestriction
)

Restricts the autocomplete results to a particular area.

setOnPlaceSelectedListener

public AutocompleteSupportFragment setOnPlaceSelectedListener(@Nullable PlaceSelectionListener listener)

Sets a listener that will be notified when a place is selected.

Parameters
@Nullable PlaceSelectionListener listener

The listener that should be notified.

setPlaceFields

public AutocompleteSupportFragment setPlaceFields(List<Place.Field> placeFields)

Sets the desired fields of the Place object returned from selecting an autocomplete result.

Read more about Place Data Fields.

setRegionCode

public AutocompleteSupportFragment setRegionCode(@Nullable String regionCode)

Sets the unicode country/region code (CLDR) of the location where the request is coming from.

The region code field is only supported by Places API (New). Please enable your API key for the Places API (New) in the Google Cloud Console to use region code. If you do not enable your API key for the Places API (New), the region code will not affect the results.

setText

public AutocompleteSupportFragment setText(@Nullable CharSequence text)

Sets the text to display in the search input field.

setTypeFilter

public AutocompleteSupportFragment setTypeFilter(@Nullable TypeFilter typeFilter)

Restricts the autocomplete results to the given place type.

setTypesFilter

public AutocompleteSupportFragment setTypesFilter(List<String> placeTypes)

Sets the filter that restricts the type(s) of results returned from the response.

Up to 5 place types are supported. A comprehensive list of the supported types can be found in com.google.android.libraries.places.api.model.PlaceTypes. The type collections specified in table 3 must be passed in as solo entries and cannot be combined with any other entries from table 1 or 2.

This will override setTypeFilter if both are used.

Extension functions

AutocompleteSupportFragmentKt.placeSelectionEvents

public final @NonNull Flow<@NonNull PlaceSelectionResultAutocompleteSupportFragmentKt.placeSelectionEvents(
    @NonNull AutocompleteSupportFragment receiver
)

Returns a Flow of PlaceSelectionResults that are emitted when a place is selected or an error occurs.