Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enabling Button when EditText has text (RxAndroid)

New to RxJava/RxAndroid and am finding the lack of examples disturbing. As a way to jump into using Rx, id like to try to get something small working. Basically, if an EditText has text entered into it, then enable a Button below it.

I came across this answer, but the authors edit doesnt really show how to completely implement something like this.

From what I've gathered, I can use RxBindings to create an Observable like:

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

Supposedly I would now need to .subcribe() an Observer to watch for changes in observable, but am unsure how this would be done.

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

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

like image 947
Orbit Avatar asked Dec 23 '15 08:12

Orbit


2 Answers

In order to subscribe to 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're not using retrolambda, you could 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);
    }

});

As for the second part of your question: to be honest, I'm not sure but I would guess that you would add a TextWatcher on the EditText and fire an event each time the text changes (using Observable.just(charSequenceAfterEdit)).

like image 173
W.K.S Avatar answered Oct 10 '22 21:10

W.K.S


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);
        }
    });

Don't forget to keep a reference to the subscription and unsubscribe when you no longer need it (eg. in onDestroy).

RxJava-Android-Samples contains RxJava examples for Android. Check it out. You might wanna check out the Form Validation example.

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

You can check out the implementation. It's open source. Internally it uses a TextWatcher to monitor the changes and emits items when the text changes.

like image 42
LordRaydenMK Avatar answered Oct 10 '22 20:10

LordRaydenMK