You need to customize your adapter. I had implemented this functionality in my project. You can follow this.
activity_search.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/white">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:id="@+id/search_layout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:background="@drawable/searchbar_bg"
android:layout_marginLeft="@dimen/activity_margin_10"
android:layout_marginRight="@dimen/activity_margin_10"
android:layout_marginTop="@dimen/activity_margin_10"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/search_et"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Search"
android:singleLine="true"
android:layout_toLeftOf="@+id/clear"
android:imeOptions="actionSearch"
android:background="@null"
android:drawableLeft="@drawable/ic_action_search"
android:drawablePadding="@dimen/activity_margin_10"
android:paddingLeft="@dimen/activity_margin_10"
android:paddingRight="@dimen/activity_margin_10"/>
<ImageView
android:id="@+id/clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_clear"
android:layout_alignParentRight="true"
android:layout_gravity="right|center_vertical"
android:padding="@dimen/activity_margin_16"
android:visibility="gone"/>
</RelativeLayout>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/list_search"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/search_layout"
android:layout_above="@+id/powered_by_google"
android:background="@color/white"
android:layout_marginTop="@dimen/activity_margin_10"/>
<ImageView
android:id="@+id/powered_by_google"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:padding="@dimen/activity_margin_10"
android:layout_marginBottom="@dimen/activity_margin_10"
android:src="@drawable/powered_by_google_light"/>
</RelativeLayout>
</LinearLayout>
SearchActivity.java
package com.android.dezi.views.rider.Activities;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.android.dezi.BaseActivity;
import com.android.dezi.R;
import com.android.dezi.adapters.PlaceAutocompleteAdapter;
import com.android.dezi.adapters.PlaceAutocompleteAdapter.PlaceAutoCompleteInterface;
import com.android.dezi.adapters.PlaceSavedAdapter;
import com.android.dezi.adapters.PlaceSavedAdapter.SavedPlaceListener;
import com.android.dezi.beans.SavedAddress;
import com.android.dezi.views.rider.Fragments.SearchFragment;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.places.Place;
import com.google.android.gms.location.places.PlaceBuffer;
import com.google.android.gms.location.places.Places;
import com.google.android.gms.location.places.ui.PlaceAutocomplete;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import java.util.ArrayList;
import java.util.List;
/**
* Created by anuj.sharma on 4/6/2016.
*/
public class SearchActivity extends BaseActivity implements PlaceAutoCompleteInterface, GoogleApiClient.OnConnectionFailedListener,
GoogleApiClient.ConnectionCallbacks,OnClickListener,SavedPlaceListener {
Context mContext;
GoogleApiClient mGoogleApiClient;
LinearLayout mParent;
private RecyclerView mRecyclerView;
LinearLayoutManager llm;
PlaceAutocompleteAdapter mAdapter;
List<SavedAddress> mSavedAddressList;
PlaceSavedAdapter mSavedAdapter;
private static final LatLngBounds BOUNDS_INDIA = new LatLngBounds(
new LatLng(-0, 0), new LatLng(0, 0));
EditText mSearchEdittext;
ImageView mClear;
@Override
public void onStart() {
mGoogleApiClient.connect();
super.onStart();
}
@Override
public void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_search);
mContext = SearchActivity.this;
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, 0 /* clientId */, this)
.addApi(Places.GEO_DATA_API)
.build();
initViews();
}
/*
Initialize Views
*/
private void initViews(){
mRecyclerView = (RecyclerView)findViewById(R.id.list_search);
mRecyclerView.setHasFixedSize(true);
llm = new LinearLayoutManager(mContext);
mRecyclerView.setLayoutManager(llm);
mSearchEdittext = (EditText)findViewById(R.id.search_et);
mClear = (ImageView)findViewById(R.id.clear);
mClear.setOnClickListener(this);
mAdapter = new PlaceAutocompleteAdapter(this, R.layout.view_placesearch,
mGoogleApiClient, BOUNDS_INDIA, null);
mRecyclerView.setAdapter(mAdapter);
mSearchEdittext.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (count > 0) {
mClear.setVisibility(View.VISIBLE);
if (mAdapter != null) {
mRecyclerView.setAdapter(mAdapter);
}
} else {
mClear.setVisibility(View.GONE);
if (mSavedAdapter != null && mSavedAddressList.size() > 0) {
mRecyclerView.setAdapter(mSavedAdapter);
}
}
if (!s.toString().equals("") && mGoogleApiClient.isConnected()) {
mAdapter.getFilter().filter(s.toString());
} else if (!mGoogleApiClient.isConnected()) {
// Toast.makeText(getApplicationContext(), Constants.API_NOT_CONNECTED, Toast.LENGTH_SHORT).show();
Log.e("", "NOT CONNECTED");
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
@Override
public void onClick(View v) {
if(v == mClear){
mSearchEdittext.setText("");
if(mAdapter!=null){
mAdapter.clearList();
}
}
}
@Override
public void onConnected(Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
@Override
public void onPlaceClick(ArrayList<PlaceAutocompleteAdapter.PlaceAutocomplete> mResultList, int position) {
if(mResultList!=null){
try {
final String placeId = String.valueOf(mResultList.get(position).placeId);
/*
Issue a request to the Places Geo Data API to retrieve a Place object with additional details about the place.
*/
PendingResult<PlaceBuffer> placeResult = Places.GeoDataApi
.getPlaceById(mGoogleApiClient, placeId);
placeResult.setResultCallback(new ResultCallback<PlaceBuffer>() {
@Override
public void onResult(PlaceBuffer places) {
if(places.getCount()==1){
//Do the things here on Click.....
Intent data = new Intent();
data.putExtra("lat",String.valueOf(places.get(0).getLatLng().latitude));
data.putExtra("lng", String.valueOf(places.get(0).getLatLng().longitude));
setResult(SearchActivity.RESULT_OK, data);
finish();
}else {
Toast.makeText(getApplicationContext(),"something went wrong",Toast.LENGTH_SHORT).show();
}
}
});
}
catch (Exception e){
}
}
}
@Override
public void onSavedPlaceClick(List<SavedAddress> mResponse, int position) {
if(mResponse!=null){
try {
Intent data = new Intent();
data.putExtra("lat",String.valueOf(mResponse.get(position).getLatitude()));
data.putExtra("lng", String.valueOf(mResponse.get(position).getLongitude()));
setResult(SearchActivity.RESULT_OK, data);
finish();
}
catch (Exception e){
}
}
}
}
PlaceAutocompleteAdapter.java
This is customized adapter. All important part is inside this.
package com.android.dezi.adapters;
import android.content.Context;
import android.graphics.Typeface;
import android.support.v7.widget.RecyclerView;
import android.text.style.CharacterStyle;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…