Search code examples
androidbuttonontouch

Button call "main" onTouch instead of his own


im creating app where we can move button with finger. It's ok if I dont press button but if i do, he is calling his own function instead of "main" onTouch

Exemplary code:

       final Button b = new Button(this);
        b.setText("Something");
        b.setX(50);
        b.setY(50);
        b.setId((int) 1);
           b.setOnTouchListener(new View.OnTouchListener() {
           @Override
            public boolean onTouch(View v, MotionEvent event) {
              return false;
            }
        });
        layout.addView(b);

And he is calling his own function and i want him to call this:

@Override
public boolean onTouch(View v, MotionEvent event) {
    moving();
}

Is there a way to call second onTouch from Button onTouch?


Solution

  • If you implemented the "main" onTouch in the Activity

    b.setOnTouchListener(this);
    

    Otherwise, just call that method

    b.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            moving():
            return false;
        }
    });