for my app i want to determinate how long i touch the screen. I want use the function onTouchEvent, i don't want use some images or some buttons to do it. Some idea? Otherwise, if there isn't a way, with a buttons but not with a image.
You can catch the movement using MotionEvent.ACTION_DOWN to start the timer and stop it when you catch MotionEvent.ACTION_UP.
#Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN){
//start timer
} else if(event.getAction()==MotionEvent.ACTION_UP){
//end timer
}
return true;
}
ZouZou's answer is good, but here's another solution, without struggling with timers. If you have a MotionEvent event then you can use getDownTime() and getEventTime() methods. The difference between them will be the duration of the touch.
#Override
public boolean onTouchEvent(MotionEvent event) {
final long time = (event.getEventTime() - event.getDownTime();
return true;
}
Related
I've got the problem, that the onTouchEvent reacts to staying with the finger on screen and move the finger. That isn`t my aim. I want, that the OnTouchEvent only reacts if i tap on the screen, after this the OnTouchEvent should reactivate itself and the user have to click another time.
Anyone can solve my problem? I mean it's a little bit tricky with my timer, because i never leave the method OnTouchEvent.
public boolean onTouchEvent(MotionEvent me){
if(!started){
started = true;
taptostart.setVisibility(View.GONE);
expla.setVisibility(View.GONE);
//start the timer
timer.schedule(new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
#Override
public void run() {
changePos();
}
});
}
}, 0, 20);
}else{
activateHo();
}
return false;
}
public boolean onTouchEvent(MotionEvent me){
if(me.getAction() == MotionEvent.ACTION_DOWN){
//Do what you want here, when the view is touched
}
return false;
}
I am creating a custom UI component where user can upload an image of a map and then user can specify certain locations and Display a pin. First upon image of the map loaded user will be able to pan and zoom to any position. for this I used following example which works like a charm.
TouchImageView
Next part was to add a pin when it is long pressed on the image. For this I used an Absolute Layout over the map image and added an OnTouchListener and Returned false at the end for the touch listener on ImageView to work as well. I implemented the OnTouchListener As below
absoluteLayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, final MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
count=-1;// reset count. Thredd will start as 0
released = false;// Touch long press is started
final Handler handler = new Handler();
new Thread(new Runnable() {
#Override
public void run() {
while(!released){//Stop count if touch released
try {
count++;// Count every 200 ms
Thread.sleep(200);
if(count>10){ //if pressed for 200*10= 2 seconds
released=true;
handler.post(new Runnable() {
#Override
public void run() {
addPin((int) event.getX(), (int) event.getY());//Calling the method to add the pin
}
});
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
break;
case MotionEvent.ACTION_UP:
released=true;// touch is released
break;
case MotionEvent.ACTION_MOVE:
released=true; // touch is a movement therefore release long press
break;
}
return false;
}
});
But the issue here is With case :"MotionEvent.ACTION_MOVE" it always make the long press released. But without it if user is trying to pan or zoom the imageView it will add a pin.
How can i detect if there is a movement in the touch and ignore when user is zooming or panning the map?
Thanks in advance.
You can specify a minimum amount of movement to be sure if it's an accidental movement or not, like it's explained here
I am using the android developer tools in Eclipse, programming in Java, and I need to make an object move across the screen as long as a button is pressed. I've been doing research for hours, and I cannot find any methods to accomplish this. I've tried running threads, which often crash or seemingly don't execute. I've also tried an onClickListener which reads the button state and uses it to determine whether or not the button is still pressed. I'm currently using a while loop, but this just freezes the program. I believe that this is the best method, and I've tried to use Thread.sleep in order to limit the number of iterations per second, as I believe that this is the reason it is freezing. Am I on the right track or am I way off in left field? Here is a snippet of code:
rightButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
while(arg0.isPressed())
{
mover.updateCoordinates(1, 0);
}
}
});
Would you try this another method?
Firstly declare your button as class variable, declare a Handler and a Runnable:
private Button rightButton; // You will assign this in onCreate() method
private Handler mHandler = new Handler();
private Runnable mRunnable = new Runnable() {
#Override
public void run() {
if(rightButton.isPressed())
{
// If press state is pressed, move your item and recall the runnable in 100 milliseconds.
mover.updateCoordinates(1, 0);
mHandler.postDelayed(mRunnable, 100);
}
}
};
Then your button's onClickListener will looks like this:
rightButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0)
{
// Instead of performing a loop here, just call a runnable, do simple press state checking there.
mHandler.postDelayed(mRunnable, 100);
}
});
Create a loop that updates the views, waits, and calls itself after it finishes waiting. Within the loop, have an animation if statement with a boolean field that move on true and does not move on false. Have the onClick method toggle the boolean field's value.
Try:
myView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent e) {
if (e.getAction() == MotionEvent.ACTION_DOWN) {
// Start your animation here
} else if (e.getAction() == MotionEvent.ACTION_UP) {
// Stop your animation here
}
return false;
}
});
References:
MotionEvent
OnTouchListener
Alternatively, override onKeyUp and onKeyDown of the View class and check for the KEYCODE_ENTER KeyEvent
I would like to start a timer that begins when a button is first pressed and ends when it is released (basically I want to measure how long a button is held down). I'll be using the System.nanoTime() method at both of those times, then subtract the initial number from the final one to get a measurement for the time elapsed while the button was held down.
(If you have any suggestions for using something other than nanoTime() or some other way of measuring how long a button is held down, I'm open to those as well.)
Thanks!
Andy
Use OnTouchListener instead of OnClickListener:
// this goes somewhere in your class:
long lastDown;
long lastDuration;
...
// this goes wherever you setup your button listener:
button.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
lastDown = System.currentTimeMillis();
} else if (event.getAction() == MotionEvent.ACTION_UP) {
lastDuration = System.currentTimeMillis() - lastDown;
}
return true;
}
});
This will definitely work:
button.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
increaseSize();
} else if (event.getAction() == MotionEvent.ACTION_UP) {
resetSize();
}
return true;
}
});
In onTouchListener start the timer.
In onClickListener stop the times.
calculate the differece.
What I am looking for is to have a button where:
If you just push it, does nothing.
If you push and hold it for, let's say, 3 seconds, perform an action().
Any idea of what to do?
Thank you in advance.
If you want to be able to specify the time, here is an implementation of OnTouchListener that does that for you, without using a Timer:
class TimedTouchListener implements OnTouchListener{
private final long millisRequired = 3000;
private long downTime;
#Override
public boolean onTouch(View v, MotionEvent event){
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
downTime = System.currentTimeMillis();
return true;
break;
case MotionEvent.ACTION_UP:
long upTime = System.currentTimeMillis();
if( upTime - downTime > millisRequired ){
doAction(); //doAction can be a method call, or any code you want to be executed.
return true;
}else{
return true;
}
}
return false;
}
}
If you don't need to specify the time, just go with an OnLongClickListener as suggested by others.
button.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent event) {
// remeber here that ACTION_DOWN has occured
// set the timer for 3 seconds
// if ACTION_UP occured and timer has elapsed, then call action().
}});
or as ntc noticed, you can use OnLongClickListener instead.
Thats what the LongClickListener is for: http://developer.android.com/reference/android/view/View.OnLongClickListener.html
on finger down start a 3 second timer.
on finger up if timer is still active cancel it.
on timer expire run action.