Reputation: 110570
In android,
How can I repeat calls a function from the time user press a button until he/she release that button?
I have check clickListener and longclicklistener, but it does not seem like they do what I want.
Thank you.
Upvotes: 4
Views: 998
Reputation: 117597
You can use OnTouchListener
:
public class MainActivity extends Activity implements OnTouchListener
{
private Button button;
// ...
@Override
public void onCreate(Bundle savedInstanceState)
{
// ...
button = (Button) findViewById(R.id.button_id);
button.setOnTouchListener(this);
// ...
}
// ...
@Override
public boolean onTouch(View v, MotionEvent event)
{
/* get a reference to the button that is being touched */
Button b = (Button) v;
/* get the action of the touch event */
int action = event.getAction();
if(action == MotionEvent.ACTION_DOWN)
{
/*
A pressed gesture has started, the motion contains
the initial starting location.
*/
}
else if(action == MotionEvent.ACTION_UP)
{
/*
A pressed gesture has finished, the motion contains
the final release location as well as any intermediate
points since the last down or move event.
*/
}
else if(action == MotionEvent.ACTION_MOVE)
{
/*
A change has happened during a press gesture (between
ACTION_DOWN and ACTION_UP). The motion contains the
most recent point, as well as any intermediate points
since the last down or move event.
*/
}
else if(action == MotionEvent.ACTION_CANCEL)
{
/*
The current gesture has been aborted. You will not
receive any more points in it. You should treat this
as an up event, but not perform any action that you
normally would.
*/
}
}
}
Upvotes: 2
Reputation: 132982
use OnTouchListener for calling function until button release by user as:
private OnTouchListener otl_conn = (OnTouchListener) new TouchListenerConn();
private Button bv = null;
bv = (Button) findViewById(R.id.xxxxbutton);
bv.setOnTouchListener(otl_conn);
class TouchListenerConn implements OnTouchListener
{
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
//call function here
break;
case MotionEvent.ACTION_UP:
//DO SOMETHING
xxxx....;
break;
}
return true;
}
}
Upvotes: 0
Reputation: 1706
If I'm right: Click: The user pressed and released his finger. Touch: The user's finger is still on the device.
Maybe you should try with onTouchListener? I've never used it...
Upvotes: 0