I have 3 EditText fields and I have created 3 observables for these fields.
    Observable<CharSequence> o1 = RxTextView.textChanges(field1);
    Observable<CharSequence> o2 = RxTextView.textChanges(field2);
    Observable<CharSequence> o3 = RxTextView.textChanges(field3);
I want to enable a button when all these three fields has some value in it. user can enter values in any order in the fields. How can i do that?
I used zip to achieve that.
Observable<CharSequence> o1 = RxTextView.textChanges(field1);
Observable<CharSequence> o2 = RxTextView.textChanges(field2);
Observable<CharSequence> o3 = RxTextView.textChanges(field3);
Observable.zip(o1, o2, o3, (c1, c2, c3) -> c1.length() > 0 && c2.length() > 0 && c3.length() > 0).subscribe(myButton::setEnabled)
this above case works when i enter something in all three text fields. e.g. i entered 1 character in all three text fields then the button will be enabled. But when I delete a character in any of the three fields. zip will not get called as it will be waiting for the other 2 textfields to stream some data before it calls onNext on the subscriber. so when I delete any character in any textfield I want my button to get disabled again. How can I achieve that?
Use CombineLatest instead of zip http://reactivex.io/documentation/operators/combinelatest.html
Try this, this will definately gonna work. use combineLatest.
//declare global variable
 private Subscription subscription = null;
 Observable<CharSequence> o1 = RxTextView.textChanges(field1);
 Observable<CharSequence> o2 = RxTextView.textChanges(field2);
 public void combineEvent(){
 subscription = Observable.combineLatest(o1, o2,
    new Func2<CharSequence, CharSequence, Boolean>() {
      @Override public Boolean call(CharSequence newEmail, CharSequence  newPassword) {
    //here you can validate the edit text
      boolean emailValid= !TextUtils.isEmpty(newEmail)
          &&    android.util.Patterns.EMAIL_ADDRESS.matcher(newEmail).matches();
        if(!emailValid){
          etEmailAddress.setError("Invalid Email");
        }
        boolean passValid = !TextUtils.isEmpty(newPassword)
            && newPassword.length() >6;
        if(!passValid){
          etPassword.setError("invalid password");
        }
        return emailValid && passValid;
      }
      }).subscribe(new Observer<Boolean>() {
    @Override public void onCompleted() {
    }
    @Override public void onError(Throwable e) {
  }
  @Override public void onNext(Boolean aBoolean) {
    if(aBoolean){
      //here you can enable your button or what ever you want.
      loginBtn.setEnabled(true);
    }else {
      loginBtn.setEnabled(false);
    }
  }
});
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With