Reputation: 1711
I want to implement search toolbar like in native google maps app. How can I make this? May be there is native way to implement this feature?
Upvotes: 23
Views: 49475
Reputation: 12382
If you want to get view Like Search View of Google map. Then you can use anyone of below two awesome library.
Add below dependencie to your gradle file:
compile 'com.github.arimorty:floatingsearchview:2.0.3'
Example:
<com.arlib.floatingsearchview.FloatingSearchView
android:id="@+id/floating_search_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:floatingSearch_searchBarMarginLeft="@dimen/search_view_inset"
app:floatingSearch_searchBarMarginTop="@dimen/search_view_inset"
app:floatingSearch_searchBarMarginRight="@dimen/search_view_inset"
app:floatingSearch_searchHint="Search..."
app:floatingSearch_suggestionsListAnimDuration="250"
app:floatingSearch_showSearchKey="false"
app:floatingSearch_leftActionMode="showHamburger"
app:floatingSearch_menu="@menu/menu_main"
app:floatingSearch_close_search_on_keyboard_dismiss="true"/>
2) SearchView
Add below dependencie to your gradle file:
compile 'com.lapism:searchview:4.0'
Example:
<com.lapism.searchview.SearchView
android:id="@+id/searchView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Upvotes: 10
Reputation: 564
You can use EditText
or TextWatcher
. I used an EditText
and a Search button. With this on button click, it search location.
Here is my code on button click :
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String g = searchview.getText().toString();
Geocoder geocoder = new Geocoder(getBaseContext());
List<Address> addresses = null;
try {
// Getting a maximum of 3 Address that matches the input
// text
addresses = geocoder.getFromLocationName(g, 3);
if (addresses != null && !addresses.equals(""))
search(addresses);
} catch (Exception e) {
}
}
});
and here is search method
protected void search(List<Address> addresses) {
Address address = (Address) addresses.get(0);
home_long = address.getLongitude();
home_lat = address.getLatitude();
latLng = new LatLng(address.getLatitude(), address.getLongitude());
addressText = String.format(
"%s, %s",
address.getMaxAddressLineIndex() > 0 ? address
.getAddressLine(0) : "", address.getCountryName());
markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title(addressText);
map1.clear();
map1.addMarker(markerOptions);
map1.moveCamera(CameraUpdateFactory.newLatLng(latLng));
map1.animateCamera(CameraUpdateFactory.zoomTo(15));
locationTv.setText("Latitude:" + address.getLatitude() + ", Longitude:"
+ address.getLongitude());
}
Edited- for xml code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.google_map.MainActivity" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
class="com.google.android.gms.maps.SupportMapFragment" />
<TextView
android:id="@+id/latlongLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignRight="@+id/searchView1"
android:background="#ff058fff"
android:gravity="bottom"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp"
android:textColor="#ffffffff" />
<EditText
android:id="@+id/searchView1"
android:layout_width="250dp"
android:layout_height="34dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_marginTop="14dp"
android:hint="Search Location"
android:textColor="@android:color/black"
android:background="@android:color/white" >
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="28dp"
android:layout_height="25dp"
android:layout_alignBaseline="@+id/searchView1"
android:layout_alignBottom="@+id/searchView1"
android:layout_alignRight="@+id/searchView1"
android:background="@drawable/g_search_btn" /></RelativeLayout>
Upvotes: 14
Reputation: 1641
This might help somebody who want to add search bar along with the Google maps Google Places API
Upvotes: 1
Reputation: 7087
You should go for use AutoSuggestEditext and give background effect like google have and put left and right image by Relativelayout
Here is example for Auto Suggest for place search .
Hope from it you will understand.
Upvotes: 7