I'm trying to programmatically trigger a swipe in a view. There are other questions with answers but they haven't worked for me. I also saw some comments that said programmatic dragging/scrolling was disabled for security reasons, is that true? I'd love to find a definitive answer on this.
I've tried the following method that the poster showed working via a video, so it should work! Is there something I need in my AndroidManifest
? I've also tried the scrollBy()
API but that doesn't scroll my content, and instead moves the content offscreen.
What's more, I've registered a OnTouchListener
with my view and seen that the below code copies the same format of event firing (ACTION_DOWN
,ACTION_MOVE
, ACTION_UP)
that a click+drag, and mouse wheel do, though the click+drag and a mouse wheel works while the programmatic scroll/swipe does not.
final Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
final MotionEvent event = MotionEvent.obtain(System.currentTimeMillis(), System.currentTimeMillis(), MotionEvent.ACTION_DOWN, 500, 700, 0);
dispatchTouchEvent(event);
event.recycle();
}
});
handler.postDelayed(new Runnable() {
@Override
public void run() {
final MotionEvent event = MotionEvent.obtain(System.currentTimeMillis(), System.currentTimeMillis(), MotionEvent.ACTION_MOVE, 500, 700 ,0);
dispatchTouchEvent(event);
event.recycle();
}
}, 50);
handler.postDelayed(new Runnable() {
@Override
public void run() {
final MotionEvent event = MotionEvent.obtain(System.currentTimeMillis(), System.currentTimeMillis(), MotionEvent.ACTION_MOVE, 500 ,700 + 400, 0);
dispatchTouchEvent(event);
event.recycle();
}
}, 100);
handler.postDelayed(new Runnable() {
@Override
public void run() {
final MotionEvent event = MotionEvent.obtain(System.currentTimeMillis(), System.currentTimeMillis(), MotionEvent.ACTION_UP, 500, 700 + 400, 0);
dispatchTouchEvent(event);
event.recycle();
}
}, 1000);