How to remove search icon from PlaceAutocompleteFragment in Android

最后都变了- 提交于 2019-12-01 04:47:11

问题


I am using PlaceAutocompleteFragment to search places. Below is my xml code

<fragment
android:id="@+id/place_source"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@color/colorPrimary"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment" />

I want to remove search icon which by default comes with fragment.


回答1:


You can use this single line to get the reference of search icon :((View)findViewById(R.id.place_autocomplete_search_button)).setVisibility(View.GONE);

and this line to get the reference of EditText: ((EditText)findViewById(R.id.place_autocomplete_search_input))




回答2:


If we have 2 autocompleteFragments, we can hide the icons like this :

autocompleteFragment_from.getView().findViewById(R.id.place_autocomplete_search_button).setVisibility(View.GONE);
autocompleteFragment_to.getView().findViewById(R.id.place_autocomplete_search_button).setVisibility(View.GONE);



回答3:


It's not possible to customize the look of the PlaceAutocompleteFragment unfortunately but, since all it does is launch the PlaceAutocomplete activity in response to taps it's pretty easy to replicate its functionality with a UI that works for your app.

  1. Create a custom fragment (instructions).
  2. Add the UI elements you want to the fragment.
  3. Add an OnClickListener to the UI elements that starts an autocomplete Activity (as described in the API docs).



回答4:


This question is a bit old, but I'll leave this here for future viewers.

While @rajat44's answer solves it when you have one PlaceAutocompleteFragment, if you have multiple you can do this:

pacf.getView().findViewById(R.id.place_autocomplete_search_button).setVisibility(View.GONE);
pacf2.getView().findViewById(R.id.place_autocomplete_search_button).setVisibility(View.GONE);


来源:https://stackoverflow.com/questions/42211272/how-to-remove-search-icon-from-placeautocompletefragment-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!