2

The Android input event documentation here seems to imply that returning false in an event listener will cause the event to continue on to other listeners. For example

OnTouchListener touchListener = new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // should continue on to other views
    return false;
    }
};

The problem is if I have two buttons in the same layout and I click on one I would expect the event to continue on to all views since I'm returning false, for example:

    Button submitButton = (Button) findViewById(R.id.submit_button);
    submitButton.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            System.out.println("--> submitButton");
            return false;
        }

    });

    Button clearButton = (Button) findViewById(R.id.clear_button);
    clearButton.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            System.out.println("--> clearButton");
            return false;
        }

    });

Since the submit button's listener returns false I would expect the click result to be sent on to the clear button but the output is always:

--> submitButton
--> submitButton

Can someone explain how events are propagated within a view since I seem to be misunderstanding the article?

William Seemann
  • 3,440
  • 10
  • 44
  • 78
  • 1
    You can try with OnClickListener instead of OnTouchListener.. – Vishesh Chandra Jun 18 '13 at 06:08
  • It's a theoretical question. I'm trying to confirm the behavior outlined in the documentation. Also, onClick has no relation to this scenario since that listener doesn't require a return value. – William Seemann Jun 18 '13 at 06:38

1 Answers1

1

The events are propagated from the parent views to the child views. Never to sibling views.

What you are trying would work if your clearButton were inside the submitButton (witch is not even possible).

You could mimic that behaviour with performClick()

Tiago A.
  • 2,568
  • 1
  • 22
  • 27