javascriptandroidandroid-studiobuttonmotionevent

Android studio click on coords x y


Hi i want when i clicked on button automatically button2 is click with MotionEvent me = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 500, 935, 0);

    button=(Button)findViewById(R.id.boutton);
    button2=(Button)findViewById(R.id.button2);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            MotionEvent me = MotionEvent.obtain(SystemClock.uptimeMillis(),
                    SystemClock.uptimeMillis(),
                    MotionEvent.ACTION_DOWN,
                    500,
                    935,
                    0);
            view.dispatchTouchEvent(me);

        }
    });
    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(MainActivity.this,"Clicled",Toast.LENGTH_LONG).show();
        }
    });

};

Solution

  • You will need to use performClick() method to achieve this by using it inside the onClickListener of button

    View.performClick();
    

    Please check the following answers for more understanding:

    How to simulate a button click using code?

    Simulate Android button click programmatically

    Also check the following documentation: (https://developer.android.com/reference/android/view/View.html#performClick())