onTouchEvent and onClickListener can't work together on the same page

70 Views Asked by At

For example, if I have some buttons with onclick listener and the rest of the screen recieves ontouch event.The ontouch event will jam the onclick event unless you lift up the ontouch finger the onclick will not respond.

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);        
    mAttackButton        = findViewById(R.id.attack);
    mJumpButton          = findViewById(R.id.jump);
    mAttackButton.setOnClickListener(this);
    mJumpButton  .setOnClickListener(this);}

   @Override
   public void onClick(View view) {
    switch (view.getId()) {
        case R.id.attack:
        attack();
        break;
        case R.id.jump;
        jump();
        break;
     }
   }

  @Override
  public boolean onTouchEvent(MotionEvent event) {
     super.onTouchEvent(event);
     mTouchPoint.x=event.getX();
     mTouchPoint.y=event.getY();
     return true;
 }
}
1

There are 1 best solutions below

0
idobarabi On

You can always split the onClickListener(). Instead of implementing the entirety of the activity. Try adding onClickListener() to each view individually. Same with the OnTouchEvent().