5

I am using Option 2: Use an intent to launch the autocomplete activity

Is it possible to change the "search" place holder text to something such as "enter your city". I found a solution but for javascript, suppose support android as well.

It seems it could be done with a PlaceAutocompleteFragment, but I am using my own EditText to launch autocomplete by using an intent, so it is not helpful.

enter image description here

thanhbinh84
  • 17,876
  • 6
  • 62
  • 69
  • Are you using AutoCompleteTextView for this? – R.R.M Jul 28 '17 at 06:14
  • 1
    No, I am using normal EditText view, when users click on it, it will launch autocomplete activity. Intent intent = new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY) .setFilter(typeFilter) .build(this); startActivityForResult(intent, PLACE_AUTOCOMPLETE_REQUEST_CODE); – thanhbinh84 Jul 28 '17 at 06:19
  • You can use AutoCompleteTextView for that. You will also don't have to jump to another activity if you use this. – R.R.M Jul 28 '17 at 06:21
  • As mobile screen is small, I think using new activity will provide better UX. – thanhbinh84 Jul 28 '17 at 21:12

9 Answers9

10

No there is no way and if you want than you have to create your own. But as u use the EditText(mention in Comment Section) so may be this technique is useful for your.

Code:

PlaceAutocompleteFragment autocompleteFragment;
autocompleteFragment = (PlaceAutocompleteFragment)
            getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
    autocompleteFragment.setOnPlaceSelectedListener(this);
    autocompleteFragment.setHint("Search New Location");

XMl :

<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/left_drawable_padding"
android:layout_marginTop="@dimen/margin_five"
android:layout_marginLeft="@dimen/margin_five"
android:layout_marginRight="@dimen/margin_five"
android:background="@drawable/search_background"
android:orientation="vertical">
    <fragment
        android:id="@+id/place_autocomplete_fragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
        />

and as i used android:background="@drawable/search_background" so u have to create your own background shape(acc. to your need).

first picture : enter image description here

Second picture : enter image description here

thrid picture : enter image description here

Fourth picture : enter image description here

Dhruv Tyagi
  • 814
  • 1
  • 9
  • 28
  • Sorry, I didn't have time to check the answer that time, but I meant how to change the 'search' text in the second picture. – thanhbinh84 Aug 21 '17 at 09:06
2

Unfortunately, there is no way to change the hint text with the autocomplete intent. You need to make your own widget, like the PlaceAutocompleteFragment.

One Code Man
  • 224
  • 2
  • 5
2

It seems there is no way to change placeholder text with either auto complete activity or even with PlaceAutocompleteFragment because it also use autocomplete activity inside. The only way is using AutoCompleteTextView that you have full control on it. Google provides full sample code that is also easy to integrate and provide good UX.

https://github.com/googlesamples/android-play-places/tree/master/PlaceCompleteAdapter

I create a feature request here, hopefully it will be supported in the future.

thanhbinh84
  • 17,876
  • 6
  • 62
  • 69
1
    //places auto complete

    String apiKey = getString(R.string.api_key);

    /**
     * Initialize Places. For simplicity, the API key is hard-coded. In a production
     * environment we recommend using a secure mechanism to manage API keys.
     */
    if (!Places.isInitialized()) {
        Places.initialize(getApplicationContext(), apiKey);
    }

// Create a new Places client instance. PlacesClient placesClient = Places.createClient(this);

    // Initialize the AutocompleteSupportFragment.
    AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment)
            getSupportFragmentManager().findFragmentById(R.id.autocomplete_fragment);

    autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.LAT_LNG));
    autocompleteFragment.setHint("Set Drop off");
1

You can do like this :-

val autocompleteFragment = supportFragmentManager.findFragmentById(R.id.autocomplete_fragment)
                      as AutocompleteSupportFragment


autocompleteFragment.setHint("Search New Location")
DmitryArc
  • 4,757
  • 2
  • 37
  • 42
0

Is this what you looking for?

SearchView searchView = (SearchView) 
menu.findItem(R.id.menu_search).getActionView();
searchView.setQueryHint("Your text");
Miky Braun
  • 21
  • 6
0

You can achieve google places functionality by two ways : 1.) By using PlaceAutocomplete.IntentBuilder 2.) By using PlaceAutocompleteFragment

CASE 1 :

      private void openPlacesSearch()
{
private static final int PLACE_AUTOCOMPLETE_REQUEST_CODE = 1001;

        try {
                            Intent intent =
                                    new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_FULLSCREEN)
                                            .zzih("Enter pickup location") //set the hint text here
                                            .build(this);

                            startActivityForResult(intent, PLACE_AUTOCOMPLETE_REQUEST_CODE);
                        }
                        catch (GooglePlayServicesRepairableException e) {
                            // TODO: Handle the error.
                        } catch (GooglePlayServicesNotAvailableException e) {
                            // TODO: Handle the error.
                        }
}

CASE 2: a.) Create a new Activity named "AddressSelectActivity.java" b.) Don't forget to define that activity in manifest.xml

Here is the code below :

public class AddressSelectActivity extends Activity {

    private static final String TAG = AddressSelectActivity.class.getSimpleName();
    private static final int PLACE_AUTOCOMPLETE_REQUEST_CODE = 1001;
    private PlaceAutocompleteFragment autocompleteFragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address_select);
        addAddressSuggestionListener();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode)
        {
            case PLACE_AUTOCOMPLETE_REQUEST_CODE:

                if (resultCode == RESULT_OK) {
                    Place place = PlaceAutocomplete.getPlace(this, data);
                    Log.i(TAG, "Place: " + place.getName());

                    LatLng latLng = place.getLatLng();

                } else if (resultCode == PlaceAutocomplete.RESULT_ERROR) {
                    Status status = PlaceAutocomplete.getStatus(this, data);
                    // TODO: Handle the error.
                    Log.i(TAG, status.getStatusMessage());

                } else if (resultCode == RESULT_CANCELED) {
                    // The user canceled the operation.
                }

                break;
            default:
                break;
        }
    }

    private void addAddressSuggestionListener()
    {
        autocompleteFragment = (PlaceAutocompleteFragment)
                getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
autocompleteFragment.setHint("Search Your Location...");

        autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
            @Override
            public void onPlaceSelected(Place place) {
                // TODO: Get info about the selected place.
                Log.i(TAG, "Place: " + place.getName());
            }

            @Override
            public void onError(Status status) {
                // TODO: Handle the error.
                Log.i(TAG, "An error occurred: " + status);
            }
        });
    }
}
Maddy Sharma
  • 4,870
  • 2
  • 34
  • 40
  • If you are able to find build( ) method, search a method in that class which facilitate for the same. Go to that class and search manually. – Maddy Sharma Jan 23 '18 at 04:53
0

If you are using an auto complete text view, use setHint on the layout control, and the top-left title and hint text will both update. If you just update the contained edit view, the top-left title will not change.

-2

Yes it is possible, do it like this

EditText et = (EditText)findViewById(R.id.place_autocomplete_search_input);
    et.setHint("enter your city");
Gentle
  • 519
  • 6
  • 12