I am trying to inflate an AutoCompleteSupportFragment which uses Google Places API, in an onClick event of an editText.
I want to inflate that fragment when the editText is clicked, so that the user might enter a location, select from the suggestions in the auto complete fragment and the location name will be shown on the editText.
I checked this link here, but it didnt work for me.
places_auto_complete_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/place_autocomplete_fragment"
android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment" />
how I am trying to inflate it
sourceEditText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
LayoutInflater layoutInflater = getLayoutInflater();
layoutInflater.inflate(R.layout.places_auto_complete_fragment,addTripLinearLayout,false);
}
});
Any help is appreciated. Thanks
Use Intent builder instead fragment in editText....
initialise on Oncreate
// Initialize Places.
Places.initialize(getApplicationContext(), "***YOUR API KEY***");
// Create a new Places client instance.
PlacesClient placesClient = Places.createClient(this);
Then use below code on edittext on click event...
List<Place.Field> fields = Arrays.asList(Place.Field.ID, Place.Field.NAME);
// Start the autocomplete intent.
Intent intent = new Autocomplete.IntentBuilder(AutocompleteActivityMode.FULLSCREEN, fields).build(this);
startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Place place = Autocomplete.getPlaceFromIntent(data);
Log.i(TAG, "Place: " + place.getName() + ", " + place.getId());
} else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
// TODO: Handle the error.
Status status = Autocomplete.getStatusFromIntent(data);
Log.i(TAG, status.getStatusMessage());
} else if (resultCode == RESULT_CANCELED) {
// The user canceled the operation.
}
}
}
Please refer the below link https://stackoverflow.com/a/55045772/10579969...
if you are using only fragment please refer below code
AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment)
getSupportFragmentManager().findFragmentById(R.id.autocomplete_fragment);
autocompleteFragment.setCountry("IN"); //country type
autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME));
//to indicate the types of place data that you want to get.
AutocompleteSupportFragment presents a search box button to the user, which presents a search box UI when clicked
Inflating the layout loads the UI but you have to add it to your view in order to make it visible.
Related
I am using this Barcode Scanner Library Dependenceny in my code.
implementation 'com.journeyapps:zxing-android-embedded:4.3.0'
I am working in fragment and in fragment when i click on button there is a dialog appears and in the dialog i have an scan button to can the barcode and want to show the same text inside the the dialog's Textview. My dialog code is inside the oncreate but the ActivityResultLauncher in out of scope so how can i make my dialog variable to be in scope of ActivityResultLauncher. I am very thankful to all of you developer to answer this query.
here is the oncreate code :
fab_addproduct.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
bottomSheetDialog = new BottomSheetDialog(getContext(), R.style.AppBottomSheetDialogTheme);
bottomSheetDialog.setContentView(R.layout.productadd_bottom_sheet);
TextInputEditText product_id = bottomSheetDialog.findViewById(R.id.product_id);
TextInputEditText product_name = bottomSheetDialog.findViewById(R.id.product_name);
TextInputEditText product_quantity = bottomSheetDialog.findViewById(R.id.product_quantity);
MaterialButton add_btn = bottomSheetDialog.findViewById(R.id.add_btn);
ImageView barcode_scanner = bottomSheetDialog.findViewById(R.id.barcode_Scanner);
TextInputEditText product_price = bottomSheetDialog.findViewById(R.id.product_price);
barcode_scanner.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ScanOptions options = new ScanOptions();
options.setDesiredBarcodeFormats(ScanOptions.ONE_D_CODE_TYPES);
options.setPrompt("Scan a barcode");
options.setCameraId(0); // Use a specific camera of the device
options.setBeepEnabled(true);
options.setBarcodeImageEnabled(true);
barcodeLauncher.launch(options);
}
});
bottomSheetDialog.show();
}
});
here is the ActivityResultLauncher code where the barcode result data is avai
public final ActivityResultLauncher<ScanOptions> barcodeLauncher = registerForActivityResult(new ScanContract(),
result -> {
if(result.getContents() == null) {
Toast.makeText(getContext(), "Cancelled", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getContext(), "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
** product_id.settext(result.getContents());// this is not accessable i want to make it accessable. here product_id is red in the code.
** }
});
I have tried many ways by using the varaiable static its also show error. i also use binding to make it accessable but i am not able to achieve my goal.
I'm using an autocomplete_fragment to allow my users to search for places. After clicking the fragment it opens up the search menu, and you can type and the suggestions will appear. However, after clicking on any place the "onPlaceSelected" method won't run. After that clicking on the fragment won't make it respond either.
This fragment is in an activity with a map. I tried using the same fragment (same code and same layout) on another activity which only had text and it worked as expected. What could be happening in this activity that is making this behavior occur?
Here's the code showing the onCreate method, which calls some other methods which I wouldn't expect to be the source of the problem.
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Initialize Places.
Places.initialize(getApplicationContext(), "myapikey");
// Create a new Places client instance.
PlacesClient placesClient = Places.createClient(this);
// Initialize the 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.
Log.i("CHECK4", "Place: " + place.getName() + ", " + place.getId());
}
#Override
public void onError(Status status) {
// TODO: Handle the error.
Log.i("CHECK4", "An error occurred: " + status);
}
});
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapView = mapFragment.getView();
mapFragment.getMapAsync(this);
anchorView = findViewById(R.id.anchorView);
buttonRoutes = findViewById(R.id.buttonRoutes);
buttonMenu = findViewById(R.id.buttonMenu);
buttonRoutes.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent routesIntent = new Intent(MapsActivity.this, RoutesActivity.class);
startActivity(routesIntent);
}
});
buttonMenu.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent routesIntent = new Intent(MapsActivity.this, MenuActivity.class);
startActivity(routesIntent);
}
});
routeIds = getIdsList();
routeTitles = getTitlesList();
routeCamera = getCameraList();
routeZoom = getZoomList();
initializeAllRoutes();
initializeAllStops();
RRdistance = new HashMap<Pair<Integer, Integer>, Double>();
initializeRRdistance();
geocoder = new Geocoder(MapsActivity.this, Locale.getDefault());
checkFirstTime();
}
For some reason, something in this particular activity is breaking the autocompletefragment. No errors are shown, just nothing happens.
Edit: I tried removing different parts of my activity until the autocomplete fragment worked, and I managed to find what was going on that way. The OnActivityResult method for some reason is the cause of the problem. Deleting the method made the autocomplete fragment work.
The OnActivityResult method for some reason is the cause of the problem. Deleting the method made the autocomplete fragment work. I was using this method before migrating to the new places API.
I'm trying to migrate to the new Place AutoComplete using this migration guide provided by Google. Click Here for the Migration Guide.
In that, I'm following the 'Use an intent to launch the autocomplete activity', section, just before Place Picker is Deprecated Section.
When I used the
implementation 'com.google.android.libraries.places:places:1.0.0'
new client library, I did not face any problem and the place autocomplete worked just fine. But, I had to move to the Comparability Library which uses
implementation
'com.google.android.libraries.places:places-compat:1.0.0'
because I have to use a place picker too, and if I use the new client library
implementation 'com.google.android.libraries.places:places:1.0.0'
I could not use the Place Picker.
This is the code for the Place AutoComplete:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**
* 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(), "YOUR_API_KEY");
}
}
public void startAutocompleteActivity() {
// Set the fields to specify which types of place data to return.
List<Place.Field> fields = Arrays.asList(Place.Field.ID, Place.Field.NAME);
// Start the autocomplete intent.
Intent intent = new Autocomplete.IntentBuilder(
AutocompleteActivityMode.FULLSCREEN, fields)
.build(this);
startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE);
}
/**
* Override the activity's onActivityResult(), check the request code, and
* do something with the returned place data (in this example it's place name and place ID).
*/
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Place place = Autocomplete.getPlaceFromIntent(data);
Log.i(TAG, "Place: " + place.getName() + ", " + place.getId());
} else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
// TODO: Handle the error.
Status status = Autocomplete.getStatusFromIntent(data);
Log.i(TAG, status.getStatusMessage());
} else if (resultCode == RESULT_CANCELED) {
// The user canceled the operation.
}
}
}
So why are the following methods and variables undefined?
Places.isInitialized()
Field in Place.Field
Autocomplete and AutocompleteActivityMode in Intent intent = new Autocomplete.IntentBuilder(
AutocompleteActivityMode.FULLSCREEN, fields)
.build(this);
Android Studio says 'Cannot Resolve Symbol' for the above. And I have imported the correct library too: import com.google.android.libraries.places.compat.Places;
Google has recently updated their Places SDK for android, so now I'm updating my code too. I'm trying to use the AutocompleteSupportFragment to allow the user to set their address.
This is my code:
mAddressEditText = (AutocompleteSupportFragment) getSupportFragmentManager().findFragmentById(R.id.address);
mAddressEditText.setPlaceFields(Arrays.asList(Place.Field.ADDRESS, Place.Field.LAT_LNG));
mAddressEditText.setHint("Address");
mAddressEditText.setText("Test1"); // Works fine at the beginning, disappears after selecting a place and shows only the hint
mAddressEditText.setOnPlaceSelectedListener(new PlaceSelectionListener() {
#Override
public void onPlaceSelected(Place place) {
Log.d(TAG, "Place Selected");
// Other Stuff
mAddressEditText.setText("Test2"); // Doesn't Work, all I can see is the hint
mAddressEditText.setText(place.getAddress()); // Doesn't Work, all I can see is the hint
}
#Override
public void onError(Status status) {
Log.e(TAG, "An error occurred: " + status);
invalidAddressDialog.show();
}
});
In the previous SDK, the fragment would set the text to the selected address automatically. This doesn't work in the new SDK (not sure if that's intentional or not).
So I'm trying to set it manually instead. As you can see in the comments in my code, using setText works fine outside the listeners. Inside the listener they don't.
Am I doing something wrong or is this a bug?
EDIT:
So long and I still can't get a proper fix to this.
To be perfectly clear, I can get the address correctly from the fragment, the only thing that doesn't work is setText.
However, since some answers state they're not getting the same problem, I started thinking it might be related to the library versions I'm using?
These are the libraries I have in my build.gradle:
api 'com.android.support:appcompat-v7:28.0.0'
api 'com.android.support:support-annotations:28.0.0'
api 'com.android.support:multidex:1.0.3'
api 'com.google.firebase:firebase-core:16.0.8'
api 'com.google.firebase:firebase-auth:16.2.1'
api 'com.google.firebase:firebase-firestore:18.2.0'
api 'com.google.firebase:firebase-storage:16.1.0'
api 'com.google.android.libraries.places:places:1.1.0'
setText has been giving me the same problem - it must be a bug I think. However I found a little work around with the hint. In your onPlaceSelected you can put the following:
Java
EditText etPlace = (EditText) autocompleteFragment.getView().findViewById(R.id.places_autocomplete_search_input);
etPlace.setHint(place.getAddress())
Kotlin
val etPlace = autocompleteFragment.view?.findViewById(R.id.places_autocomplete_search_input) as EditText
etPlace.hint = place.address
This is the code that I am using and it is working perfectly fine.
Make some changes to build.gradle (app level)
Add this to build.gradle:
android{
...
ext {
googlePlayServicesVersion = "15.0.1"
}
}
Add those dependencies:
dependencies {
...
//Also if you're using any firebase dependencies make sure that the are up to date
implementation 'com.google.android.gms:play-services-places:16.0.0'
implementation 'com.google.android.libraries.places:places:1.1.0'
}
apply plugin: 'com.google.gms.google-services'
In xml layout:
<fragment
android:id="#+id/autocomplete_fragment"
android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
The code in Activity:
private void initGooglePlacesApi() {
// Initialize Places.
Places.initialize(getApplicationContext(), "YOUR_API_KEY");
// Create a new Places client instance.
PlacesClient placesClient = Places.createClient(getApplicationContext());
// Initialize the AutocompleteSupportFragment.
AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment)
getSupportFragmentManager().findFragmentById(R.id.autocomplete_fragment);
autocompleteFragment.setHint(getString(R.string.select_location_search_bar));
// autocompleteFragment.setLocationRestriction(RectangularBounds.newInstance(
// new LatLng(34.7006096, 19.2477876),
// new LatLng(41.7488862, 29.7296986))); //Greece bounds
autocompleteFragment.setCountry("gr");
// Specify the types of place data to return.
autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ADDRESS, Place.Field.ADDRESS_COMPONENTS));
autocompleteFragment.setTypeFilter(TypeFilter.ADDRESS);
// Set up a PlaceSelectionListener to handle the response.
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
#Override
public void onPlaceSelected(Place place) {
if(place.getAddressComponents().asList().get(0).getTypes().get(0).equalsIgnoreCase("route")){
binding.textViewLocation.setText(place.getAddress()); //Works well
location = place.getAddress();
}else{ //If user does not choose a specific place.
AndroidUtils.vibratePhone(getApplication(), 200);
TastyToast.makeText(getApplicationContext(),
getString(R.string.choose_an_address), TastyToast.DEFAULT, TastyToast.CONFUSING);
}
Log.i(TAG, "Place: " + place.getAddressComponents().asList().get(0).getTypes().get(0) + ", " + place.getId() + ", " + place.getAddress());
}
#Override
public void onError(Status status) {
Log.i(TAG, "An error occurred: " + status);
}
});
}
I found a pretty simple solution..just delay a little bit the moment you set the text in the EditText. So in your PlaceSelectionListener just do it this way:
Handler().postDelayed({
mAddressEditText.setText(place.getAddress());
}, 300)
PS: This is kotlin code but It's almost similar in Java
By setting "NAME" in setPlaceFields, a selected address is automatically shown in the fragment:
AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment)
getSupportFragmentManager().findFragmentById(R.id.autocomplete_fragment);
// Set "Place.Field.NAME" as below here to show the selected item //
autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, **Place.Field.NAME**,Place.Field.ADDRESS,Place.Field.LAT_LNG));
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
#Override
public void onPlaceSelected(#NonNull Place place) {
// TODO: Get info about the selected place. }
#Override
public void onError(#NonNull Status status) {
// TODO: Handle the error. }
});
I believe this is a bug, because it doesn't make sense to have it working like this.
What does work is set other autocomplete's texts but not its own.
This has to be a bug.
EDIT: here is my updated answer
# When you are using AutocompleteSupportFragment or AutocompleteActivity
# in Fragments, do this:
public class YourFragment extends Fragment {
/.../
#Override
public void onActivityResult (int requestCode,int resultCode,
#Nullable Intent data){
# AUTOCOMPLETE_REQUEST_CODE is just a unique constant, define it
if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
if (resultCode == AutocompleteActivity.RESULT_OK) {
Place place = Autocomplete.getPlaceFromIntent(data);
// when resultcode is RESULT_OK
mAddressEditText.setText(place.getName());
// Notice this line, update your editText up here
}else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
Status status = Autocomplete.getStatusFromIntent(data);
// Handle error
} else if (resultCode == AutocompleteActivity.RESULT_CANCELED) {
// Handle results if canceled
}
super.onActivityResult(requestCode, resultCode, data);
}
}
/.../
}
# If you are extending AppCompatActivity, you might want to do this
# ONLY when you are already doing something in onActivityResult
public class YourActivity extends AppCompatActivity{
/.../
#Override
public void onActivityResult (int requestCode,int resultCode,#Nullable Intent data){
# your logic here.
/.../
# if you are already overriding onActivityResult,
# do not forget to put this line
super.onActivityResult(requestCode, resultCode, data);
}
/.../
}
I was having the problem too. Turns out you HAVE to override this and implement it anyway, whether using AutocompleteSupportFragment or AutocompleteActivity if you are working in Fragments.
If you are using AppCompatActivity you do not have to implement it, but if you are already overiding onActivityResult to do something, do not forget to call the base method super.onActivityResult
get the reference of AutoCompleteFragment and then set text to the autocomplete fragment
like
autoCompleteFragment.setText("Address")
for reference you can have a look at the documentation
https://developers.google.com/android/reference/com/google/android/gms/location/places/ui/PlaceAutocompleteFragment
I tried CacheMeOutside's solution but it didn't work at all. So, I decided to try Matthias's solution and it did work because the text actually sets and then immediately removes for some reason. A small delay fixes it. The delay can be as small as 1 millisecond.
If my solution doesn't work for you, you can try to experiment with the delay. It also seems that it doesn't stop view rendering, so you can set any time you want.
private lateinit var autocomplete: AutocompleteSupportFragment
override fun onPlaceSelected(place: Place) {
Timer("SetAddress", false).schedule(1) {
autocomplete.setText(place.address)
}
}
The code snippet in Kotlin. If your code is in Java, just find some instrument to delay code execution for some time.
The solution by Matthias is working but it is in kotlin. Below is the same implementation in Java
#Override
public void onPlaceSelected(Place place) {
String name = place.getName()+", "+place.getAddress();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
autocompleteFragmentDestination.setText(name);
}
},300);
}
Simple. Use a Spannable string to set the color!
val autocompleteFragment = childFragmentManager.findFragmentById(R.id.location_filter_autocomplete) as AutocompleteSupportFragment
val text = SpannableString("Enter a location")
text.setSpan(ForegroundColorSpan(Color.BLACK), 0, text.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
autocompleteFragment.setHint(text)
Do the same inside onPlaceSelectedListener()
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.
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 :
Second picture :
thrid picture :
Fourth picture :
Unfortunately, there is no way to change the hint text with the autocomplete intent.
You need to make your own widget, like the PlaceAutocompleteFragment.
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.
//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");
You can do like this :-
val autocompleteFragment = supportFragmentManager.findFragmentById(R.id.autocomplete_fragment)
as AutocompleteSupportFragment
autocompleteFragment.setHint("Search New Location")
Is this what you looking for?
SearchView searchView = (SearchView)
menu.findItem(R.id.menu_search).getActionView();
searchView.setQueryHint("Your text");
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);
}
});
}
}
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.
Yes it is possible, do it like this
EditText et = (EditText)findViewById(R.id.place_autocomplete_search_input);
et.setHint("enter your city");