<p>Here is the simple example of <span style="color: #008000;"><strong>Google Places SDK</strong> </span>for Android returns place predictions in response to user search queries.As the user types, the autocomplete service returns suggestions for places such as <em><strong>businesses, addresses, latitude, longitude etc</strong></em>.</p>
<p>Get the full code from <a href="https://github.com/arunk7839/GooglePlaceAPIExample">Github</a></p>
<p> ;</p>
<p><amp-youtube layout="responsive" width="1200" height="675" data-videoid="JaQ9vukd_0Q" title="Android Place API Example"><a placeholder href="https://youtu.be/JaQ9vukd_0Q"><img src="https://i.ytimg.com/vi/JaQ9vukd_0Q/hqdefault.jpg" layout="fill" object-fit="cover" alt="Android Place API Example"></a></amp-youtube></p>
<p> ;</p>
<p>You can add <strong><span style="color: #008000;">autocompleteTextView</span></strong> to your app in the following ways:</p>
<p>1) Autocomplete widget to save development time and ensure a consistent user experience.</p>
<p>2) Get places predictions programmatically to create a customised UI.</p>
<p>Here we will go with <strong><span style="color: #008000;">Autocomplete</span></strong> widget</p>
<p><span style="color: #000080;"><strong>Obtaining Google API Key</strong></span></p>
<p>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 <strong><a class="ref-link" href="https://code.google.com/apis/console" target="_blank" rel="nofollow noopener noreferrer">Google Places APIs console</a></strong>. The same key can be used for all other google services.</p>
<p> ;</p>
<p><strong>1)After obtaining the api key add the API key in the manifest file under application tag like:</strong></p>
<p><span style="color: #008000;"><strong>AndroidManifest.xml</strong></span></p>
<pre><code><;?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"
 <strong><span style="color: #ff6600;">android:value="ADD YOUR APIKEY"</span></strong>/>;

 <;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>;</code></pre>
<p> ;</p>
<p><strong>2)Getting the Places list </strong></p>
<p><span style="color: #0000ff;"><strong>Here I am putting both files .kt(for Kotlin) and .java(for Java)</strong></span></p>
<p><span style="color: #008000;"><strong>MainActivity.kt</strong></span></p>
<!-- WP QUADS Content Ad Plugin v. 2.0.98.1 -->
<div class="quads-location quads-ad2" id="quads-ad2" style="float:none;margin:0px;">

</div>

<pre><code>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();
 }

}</code></pre>
<p><span style="color: #0000ff;"><strong>For Java Developers:</strong></span></p>
<p><span style="color: #008000;"><strong>MainActivity.java</strong></span></p>
<pre><code>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();
 }

}</code></pre>
<p> ;</p>
<p><span style="color: #008000;"><strong>activity_main.xml</strong></span></p>
<pre><code><;?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>;</code></pre>
<p> ;</p>
<p><strong>3) Run the above code</strong></p>
<p> ;</p>
<p><img class="alignnone wp-image-430 size-medium" src="https://c1ctech.com/wp-content/uploads/2018/05/Places1-171x300.png" alt="" width="171" height="300" /> <img class="alignnone wp-image-431 size-medium" src="https://c1ctech.com/wp-content/uploads/2018/05/Place2-172x300.png" alt="" width="172" height="300" /></p>
<p> ;</p>
<p> ;</p>
<p> ;</p>
<p> ;</p>
<p> ;</p>


