Power Button When EditText Has Text (RxAndroid)

New to RxJava / RxAndroid, and I find the lack of examples getting in the way. As a way to delve into the use of Rx, id would like to try something small to work. Basically, if the EditText has the text entered into it, include the Button under it.

I stumbled upon this answer, but editing the authors really doesn't show how to fully implement something like this.

From what I have compiled, I can use RxBindings to create Observable like:

 Observable<CharSequence> observable = RxTextView.textChanges(mEditText); 

Presumably I now need .subcribe() a Observer to keep track of changes in the Observable , but I'm not sure how this will be done.

Also, how would you create EditTexts Observable without using RxBindings, if necessary?

Edit: Although Retrolambda exists, answers showing how to implement this without lambdas (or both) would be helpful.

+7
android rx-java rx-android
source share
2 answers
 Observable<CharSequence> observable = RxTextView.textChanges(mEditText); observable.map(new Func1<CharSequence, Boolean>() { @Override public Boolean call(CharSequence charSequence) { return charSequence.length() > 0; } }).subscribe(new Subscriber<Boolean>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(Boolean aBoolean) { mButton.setEnabled(aBoolean); } }); 

Do not forget to leave a link to the subscription and unsubscribe when you no longer need it (for example, in onDestroy ).

RxJava-Android-Samples contains examples of RxJava for Android. Check this. You can check the form validation example .

Also, how would you create EditTexts Observable without using RxBindings, if necessary?

You can check the implementation. It is open source. Inside, it uses a TextWatcher to track changes and emits elements when the text changes.

+7
source share

To subscribe to the Observable<CharSequence> , you would do something like this.

 Observable<CharSequence> observable = RxTextView.textChanges(mEditText).skip(1); mButton.setEnabled(false) observable.subscribe(mButton -> mButton.setEnabled(true)); 

If you are not using retrolambda , you can do something like:

 Observable<CharSequence> observable = RxTextView.textChanges(mEditText).skip(1); mButton.setEnabled(false); observable.subscribe(new Action1<CharSequence>(){ @Override public void call(CharSequence c) { mButton.setEnabled(true); } }); 

Regarding the second part of your question: to be honest, I'm not sure, but I would suggest that you add a TextWatcher to EditText and TextWatcher event every time the text changes ( using Observable.just(charSequenceAfterEdit) ).

+4
source share

All Articles