Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you send a long press from an InstrumentationTestCase?

In Android, how can I send a long press from an InstrumentationTestCase? I'd like for instance to do a sendKeys(KEYCODE_DPAD_CENTER) but make that a long click.

like image 415
JRL Avatar asked Jan 31 '26 12:01

JRL


1 Answers

Don't know if this is the only/proper way, but I managed to do it this way:

public void longClickDpadCenter() throws Exception {
    getInstrumentation().sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_CENTER));
    Thread.sleep(ViewConfiguration.get(mContext).getLongPressTimeout());
    getInstrumentation().sendKeySync(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_CENTER));
}
like image 199
JRL Avatar answered Feb 03 '26 05:02

JRL



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!