Reputation: 4595
Hi and Thanks for your help.
I need a button to perform the following behavior:
when the button is pressed an action begins (in this case a view scrolls)
as long as the button is kept pressed the action continues (the view continues scrolling)
when the button is released the action (the scrolling) stops.
I have tried this, but does not work:
Button left = (Button) findViewById(R.id.left);
left.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.e("","left");
return false;
}
});
Upvotes: 0
Views: 431
Reputation: 2837
I think you are looking for a combination of OnTouchListener and MotionEvent.ACTION_DOWN
Edit:
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// TODO start scroll
break;
case MotionEvent.ACTION_UP:
// TODO stop scroll
break;
default:
break;
}
return false;
Hope this helps.. :)
Upvotes: 4
Reputation: 459
Use MotionEvent.ACTION_DOWN
and MotionEvent.ACTION_UP
for detecting action up as well as down
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//start action here
break;
}
return super.onTouchEvent(event);
}
Upvotes: 3
Reputation: 2185
make new scroll view
ScrollView sv=new ScrollView(this);
and when you click/start button. then start thread which increment int value and scroll, and on key action down. stop the thread.
//start thread which incrment value one by one
sv.scrollBy(x, y); //x the amount of pixels to scroll by horizontally
//y the amount of pixels to scroll by vertically
Upvotes: 1
Reputation: 67296
You can use MotionEvent.ACTION_DOWN
and MotionEvent.ACTION_UP
for detecting action up and down
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//start action
break;
case MotionEvent.ACTION_UP:
//stop action
break;
}
return super.onTouchEvent(event);
}
Upvotes: 1
Reputation: 10093
Button button = (Button) findViewById(R.id.button);
button.setOnTouchListener(new View.OnTouchListener() {
@Override public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
//Start your work
break;
case MotionEvent.ACTION_UP:
// stop the work
break;
case MotionEvent.ACTION_CANCEL:
// stop the work
break;
}
return false;
}
});
Upvotes: 3
Reputation: 757
By default you can use onlongclick listener, But in your case it won't give effect as its max time 1-2 sec, so apply motion event on button and apply your logic..
Upvotes: 1