0

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();
        }
    });

};
Abdoul78
  • 5
  • 2
  • Please check the following answer. [How to simulate a button click using code?](https://stackoverflow.com/questions/4553374/how-to-simulate-a-button-click-using-code) – animeshk Dec 23 '19 at 00:07

1 Answers1

0

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())

animeshk
  • 376
  • 1
  • 7