Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] rx java2 – To implement TextWatcher with RxJava Android

Posted on November 11, 2022 By

Solution 1 :

Example for observe when text changed with RxBinding library:

private val compositeDisposable = CompositeDisposable()   

val disposable = editText.textChanges()
                    .map(CharSequence::toString)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe {
                        tvResult.text = it
                    }
     compositeDisposable.add(disposable)

And don’t forget callcompositeDisposable.clear() when activity called onDestroy() or fragment called onDestroyView().

Problem :

I am new to RxJava, to learn few fundamentals I’ve created a small project wherein I am trying to observe my EditText for changes in the entered text and updating a TextView.

Here is the code:

These are the instance variables for my Observable and Observers respectively

private Observable<String> editTextObservable;
private Observer<String> textViewObserver;

I am using RxBinding and am subscribing to the text changes like this:
I am doing this in onCreate()

RxTextView.textChanges(etData).
            subscribe(new Action1<CharSequence>() {
        @Override
        public void call(CharSequence charSequence) {
            editTextObservable = Observable.create(new ObservableOnSubscribe<String>() {
                @Override
                public void subscribe(@NonNull ObservableEmitter<String> emitter) throws Throwable {
                    Log.d(TAG, "onTextChanged subscribe: " + etData.getText().toString());
                    emitter.onNext(etData.getText().toString());
                }
            });
        }
    });

And I am updating my TextView like this:

textViewObserver = new Observer<String>() {
        @Override
        public void onSubscribe(@NonNull Disposable d) {
            Log.d(TAG, "onSubscribe: ");
        }

        @Override
        public void onNext(@NonNull String value) {
            Log.d(TAG, "onNext: ");
            tvResult.setText(value);
        }

        @Override
        public void onError(@NonNull Throwable e) {
            Log.d(TAG, "onError: ");
        }

        @Override
        public void onComplete() {
            Log.d(TAG, "onComplete: ");

        }
    };

When I run the app it does not gives me any error and I can see in the logs the onTextChanged() listener being called, but the call never goes to subscribe() method and I am not able to see any result in the TextView. What am I doing wrong here?

Comments

Comment posted by Hans Wurst

Are you allowed to use an extension library for observing the onTextChange event? There is a wrapper for all kinds of events from ui elements, which will provide an observable.

READ  [FIXED] android - How to refresh the token generated by lambda API using retrofit?
Powered by Inline Related Posts

Comment posted by localhost

@HansWurst you mean RxBinding?

Comment posted by Hans Wurst

Yes, this is the library I mean.

Comment posted by localhost

@HansWurst I tried it with RxBinding but still I am not able to see the results. I have updated the question too

Android Tags:android, rx-android, rx-java2

Post navigation

Previous Post: [FIXED] Android Studios error : Unable to locate adb location
Next Post: [FIXED] android – Here Maps Navigation samples seem to be missing the Navigation Library

Related Posts

[FIXED] android – Is there a way to prevent a period from automatically being entered into an EditText? Android
[FIXED] android – Cant cast multiple recyclerview items with their ID Android
[FIXED] android – Getting some build error in SE Policy while doing AOSP build Android
[FIXED] android – Couchbase Lite pull replication fails with error in a sample Couchbase Mobile End to End testing project Android
[FIXED] java – How to solve “Expression expected” and “cannot infer argument” errors? Android
[FIXED] url – How can Android deep links be made to work from the Chrome OS browser? Android

Archives

  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can Vicks humidifier be used without filter?
  • What color is Spanish green?
  • How old is Jamie in The War That Saved My Life?
  • When should I use scalp massager for hair growth?
  • Can I put polyurethane over liming wax?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme