Here is the simple example of Google Places SDK for Android returns place predictions in response to user search queries.As the user types, the autocomplete service returns suggestions for places such as businesses, addresses, latitude, longitude etc.
Get the full code from Github
You can add autocompleteTextView to your app in the following ways:
1) Autocomplete widget to save development time and ensure a consistent user experience.
2) Get places predictions programmatically to create a customised UI.
Here we will go with Autocomplete widget
Obtaining Google API Key
In order to make requests to Google Places API you need to provide your API key. You can get your API key by going to Google Places APIs console. The same key can be used for all other google services.
1)After obtaining the api key add the API key in the manifest file under application tag like:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="c1c.googleplaceapiexample">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="ADD YOUR APIKEY"/>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
2)Getting the Places list
Here I am putting both files .kt(for Kotlin) and .java(for Java)
MainActivity.kt
package c1c.googleplaceapiexample
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.Toast
import com.google.android.gms.common.api.Status
import com.google.android.gms.location.places.Place
import com.google.android.gms.location.places.ui.PlaceAutocompleteFragment
import com.google.android.gms.location.places.ui.PlaceSelectionListener
class MainActivity : AppCompatActivity(),PlaceSelectionListener{
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val autocompleteFragment = fragmentManager.findFragmentById(R.id.autocomplete_fragment)
as PlaceAutocompleteFragment
autocompleteFragment.setOnPlaceSelectedListener(this)
}
override fun onPlaceSelected(p0: Place?) {
Toast.makeText(applicationContext,""+p0!!.name+p0!!.latLng,Toast.LENGTH_LONG).show();
}
override fun onError(status: Status) {
Toast.makeText(applicationContext,""+status.toString(),Toast.LENGTH_LONG).show();
}
}
For Java Developers:
MainActivity.java
package c1c.googleplaceapiexample;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.places.Place;
import com.google.android.gms.location.places.ui.PlaceAutocompleteFragment;
import com.google.android.gms.location.places.ui.PlaceSelectionListener;
class MainActivity extends AppCompatActivity implements PlaceSelectionListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)
getFragmentManager().findFragmentById(R.id.autocomplete_fragment);
autocompleteFragment.setOnPlaceSelectedListener(this);
}
public void onPlaceSelected(Place place) {
Toast.makeText(getApplicationContext(), "" + place.getName() + place.getLatLng(), Toast.LENGTH_LONG).show();
}
public void onError(Status status) {
Toast.makeText(getApplicationContext(), "" + status.toString(), Toast.LENGTH_LONG).show();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
>
<fragment
android:id="@+id/autocomplete_fragment"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>
3) Run the above code
Hello Astra , thanks for this usefull tutorial ….but one error i founded …..when im used this code in fragment class..then going to click a second time this fragment class…. crash the app….error is
android.view.InflateException: Binary XML file line #63: Binary XML file line #63: Error inflating class fragment
so what is solution …plz help me….