androidtextviewrx-javarx-java2rx-binding

rxbinding and click on right drawable


Is there a way to implement the on click listener on the right drawable of an EditText by using RxBinding?

The only thing I found is:

     RxTextView.editorActionEvents(mEditText).subscribeWith(new DisposableObserver<TextViewEditorActionEvent>() {
        @Override
        public void onNext(TextViewEditorActionEvent textViewEditorActionEvent) {
            int actionId = textViewEditorActionEvent.actionId();
            if(actionId == MotionEvent.ACTION_UP) {
            }

        }

        @Override
        public void onError(Throwable e) {

        }

        @Override
        public void onComplete() {

        }
    });

But in this event I can't find the info about the position of the click.

This is the way I did it by using RxJava:

public Observable<Integer> getCompoundDrawableOnClick(EditText editText, int... drawables) {
    return Observable.create(e -> {
        editText.setOnTouchListener((v, event) -> {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                for (int i : drawables) {
                    if (i == UiUtil.COMPOUND_DRAWABLE.DRAWABLE_RIGHT) {
                        if (event.getRawX() >= (editText.getRight() - editText.getCompoundDrawables()[i].getBounds().width())) {
                            e.onNext(i);
                            return true;
                        }
                    }
                }
            }
            // add the other cases here
            return false;

        });
    });

but I feel I'm reinventing the wheel


Solution

  • You're searching in the wrong place, if you need to check the touch event, use the base View touch event using RxView, then apply your logic and filter out all non required touched, for having 'clicks' on your desired location (compound drawable).
    I must admit I'm not sure I understands the for loop logic, you can use directly the UiUtil.COMPOUND_DRAWABLE.DRAWABLE_RIGHT, but anyhow followed your logic in this example:

    public Observable<Object> getCompoundDrawableOnClick(EditText editText, int... drawables) {
            return RxView.touches(editText)
                    .filter(motionEvent -> {
                        if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                            for (int i : drawables) {
                                if (i == UiUtil.COMPOUND_DRAWABLE.DRAWABLE_RIGHT) {
                                    if (motionEvent.getRawX() >= (editText.getRight() - editText.getCompoundDrawables()[i].getBounds().width())) {
                                        return true;
                                    }
                                }
                            }
                        }
                        return false;
                    })
                    .map(motionEvent -> {
                        // you can omit it if you don't need any special object or map it to 
                        // whatever you need, probably you just want click handler so any kind of notification Object will do.
                    });
        }