PlaceAutoCompleteFragment
Add this to your project to add place autocomplete functionality for example : searching places, entering destination, etc.
Usage
For a working implementation of this project see the /app folder
-
Include the library as local library project or add this dependency in your build.gradle.
dependencies { compile 'com.outlander.placeautocomplete:PlaceAutoComplete:1.0.1' }
-
Add a PlaceAutoComplete fragment container FrameLayout to your corresponding activity's xml.
<FrameLayout android:id="@+id/placeautofragment_container" android:layout_width="match_parent" android:layout_height="match_parent" />
-
Initialize the PlaceAutoCompleteFragment in the onCreate of your activity.
PlaceAutoCompleteFragment placeAutoCompleteFragment = PlaceAutoCompleteFragment.getInstance(getBaseContext(), true, false); placeAutoCompleteFragment.addOnPlaceSelectedListener(new PlaceAutoCompleteFragment.IOnPlaceSelectedListener() { @Override public void onPlaceSelected(PlaceData placeData) { //TODO: Add your action }
@Override public void onErrorOccurred(int erroCode) { //TODO: Alert user based on the errorcode // 0 - Network Issue // 1 - Zero results } }); </code> </pre>
-
Call the addFragment method to add the PlaceAutoCompleteFragment to the container.
private void addFragment(Fragment fragment) { FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction transaction = fragmentManager.beginTransaction(); transaction.add(R.id.container, fragment); transaction.addToBackStack("Any random string or even null will work"); transaction.commit(); }
-
Override the onBackPressed of the acitivty to manage your fragments accordingly. Below is a basic snippet
@Override public void onBackPressed() { if (getSupportFragmentManager().findFragmentById(R.id.container) != null) { getSupportFragmentManager().popBackStackImmediate(); } else { super.onBackPressed(); } }
-
Lastly, add the following code to your app's build.gradle under the android section
android { ... useLibrary 'org.apache.http.legacy' dataBinding { enabled = true } .. }
Ready to go!
#Developed by
Credits
Google Geocode API