Skip to content

outlander24/PlaceAutoComplete

Repository files navigation

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

  1. Include the library as local library project or add this dependency in your build.gradle.

          
            dependencies {
              compile 'com.outlander.placeautocomplete:PlaceAutoComplete:1.0.1'
            }
          
        
  2. 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"
            />
          
        
  3. 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>
    
  4. 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();
            }
          
        
  5. 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();
                }
            }
          
        
  6. 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

About

A place auto complete fragment. Add this to your project to add place autocomplete functionality for example : searching places, entering destination, etc.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages