content: package com.rxjava2.android.samples.ui.search;
  
  import android.os.Bundle;
  import android.widget.SearchView;
  import android.widget.TextView;
  
  import androidx.appcompat.app.AppCompatActivity;
  
  import com.rxjava2.android.samples.R;
  
  import java.util.concurrent.TimeUnit;
  
  import io.reactivex.Observable;
  import io.reactivex.ObservableSource;
  import io.reactivex.android.schedulers.AndroidSchedulers;
  import io.reactivex.annotations.NonNull;
  import io.reactivex.functions.Consumer;
  import io.reactivex.functions.Function;
  import io.reactivex.functions.Predicate;
  import io.reactivex.schedulers.Schedulers;
  
  /**
   * Created by amitshekhar on 15/10/17.
   */
  
  public class SearchActivity extends AppCompatActivity {
  
      public static final String TAG = SearchActivity.class.getSimpleName();
      private SearchView searchView;
      private TextView textViewResult;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_search);
          searchView = findViewById(R.id.searchView);
          textViewResult = findViewById(R.id.textViewResult);
  
          setUpSearchObservable();
      }
  
      private void setUpSearchObservable() {
          RxSearchObservable.fromView(searchView)
                  .debounce(300, TimeUnit.MILLISECONDS)
                  .filter(new Predicate<String>() {
                      @Override
                      public boolean test(String text) {
                          if (text.isEmpty()) {
                              textViewResult.setText("");
                              return false;
                          } else {
                              return true;
                          }
                      }
                  })
                  .distinctUntilChanged()
                  .switchMap(new Function<String, ObservableSource<String>>() {
                      @Override
                      public ObservableSource<String> apply(String query) {
                          return dataFromNetwork(query)
                                  .doOnError(throwable -> {
                                      // handle error
                                  })
                                  // continue emission in case of error also
                                  .onErrorReturn(throwable -> "");
                      }
                  })
                  .subscribeOn(Schedulers.io())
                  .observeOn(AndroidSchedulers.mainThread())
                  .subscribe(new Consumer<String>() {
                      @Override
                      public void accept(String result) {
                          textViewResult.setText(result);
                      }
                  });
      }
  
      /**
       * Simulation of network data
       */
      private Observable<String> dataFromNetwork(final String query) {
          return Observable.just(true)
                  .delay(2, TimeUnit.SECONDS)
                  .map(new Function<Boolean, String>() {
                      @Override
                      public String apply(@NonNull Boolean value) {
                          return query;
                      }
                  });
      }
  
  }
  