title: Google Places Autocomplete Api 2019
body: <p>I have been trying for days now and I just can't find a solution for using the Google's places autocomplete API. All the methods that I found were majority of the older version. Now, with the Google Play Services version of the Places SDK for Android (in Google Play Services 16.0.0) being deprecated, I can't seem to find a working updated code. All the solutions I find online are not working for me and I can't even understand what is happening. Can someone just elaborate the code for the new way to use the Google Place Autocomplete API.</p>
  
  <p>This is what I have done till now. </p>
  
  <pre class="lang-java prettyprint-override"><code>public class SearchAndFilter extends AppCompatActivity {
      String TAG = "placeautocomplete";
      TextView txtView;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_search_and_filter);
          txtView = findViewById(R.id.txtView);
  
          // Initialize Places.
          Places.initialize(getApplicationContext(), "MY_KEY");
          // Create a new Places client instance.
          PlacesClient placesClient = Places.createClient(this);
  
          // Initialize the AutocompleteSupportFragment.
          AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment)
                  getSupportFragmentManager().findFragmentById(R.id.autocomplete_fragment);
  
          // Specify the types of place data to return.
          autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME));
  
          // Set up a PlaceSelectionListener to handle the response.
          autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
              @Override
              public void onPlaceSelected(Place place) {
                  // TODO: Get info about the selected place.
                  txtView.setText(place.getName()+","+place.getId());
                  Log.i(TAG, "Place: " + place.getName() + ", " + place.getId());
              }
  
              @Override
              public void onError(Status status) {
                  // TODO: Handle the error.
                  Log.i(TAG, "An error occurred: " + status);
              }
          });
      }
  }
  </code></pre>
  
  <p>Layout File</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;androidx.constraintlayout.widget.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=".activity.SearchAndFilter"&gt;
      &lt;androidx.cardview.widget.CardView
          android:id="@+id/idCardView"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_margin="5dp"
          android:layout_marginTop="5dp"
          app:cardCornerRadius="4dp"&gt;
  
          &lt;fragment android:id="@+id/autocomplete_fragment"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
              /&gt;
  
      &lt;/androidx.cardview.widget.CardView&gt;
  
      &lt;TextView
          android:id="@+id/txtView"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Hello World!"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintLeft_toLeftOf="parent"
          app:layout_constraintRight_toRightOf="parent"
          app:layout_constraintTop_toTopOf="parent" /&gt;
  
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  </code></pre>
  
  <p>When I run the above code, the search bars comes with the google watermark, but just as I type in something, the bar just closes. I have no idea why.</p>