As per android MotionEvent documents: 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.
ACTION_MOVE Android doc
so when i apply a setOnTouchListene on my view is perfectly works, It give me ACTION_DOWN, ACTION_UP, and ACTION_MOVE
but my problem is i just want to ignore a ACTION_DOWN events exactly before ACTION_MOVE. Because ACTION_MOVE event occur only after the ACTION_DOWN event as per its documents.
my coding:
button.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
Log.e("Mouse: ", "Click");
break;
case MotionEvent.ACTION_MOVE:
Log.e("Mouse: ", "Move");
break;
}
return false;
}
});
So, there is any why to ignore ACTION_DOWN event. Because user only want to move not want to click, and ACTION_MOVE is also occur ACTION_DOWN before it execute it self.
Thanks...
According to your comment - you can play with counter. For example:
private static int counter = 0;
...
button.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
Log.e("Mouse: ", "Click");
break;
case MotionEvent.ACTION_MOVE:
counter++; //check how long the button is pressed
if(counter>10){
Log.e("Mouse: ", "Move");
}
break;
case MotionEvent.ACTION_UP:
if(counter<10){
//this is just onClick, handle it(10 is example, try different numbers)
}else{
//it's a move
}
counter = 0;
break;
}
return false;
}
});
Related
I am developing a game and I need to be able to detect that one finger is performing a MOVE while posibly another finger can TOUCH another part of the screen.
With the following code I am able to detect both the ACTION_MOVE (on certain region of the screen) and the ACTION_DOWN
public boolean onTouch(View v, MotionEvent event) {
final int dest_x = (int) event.getX();
final int dest_y = (int) event.getY();
onTrackPad = dbSettings.TRACK_PAD.contains(dest_x, dest_y);
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
if (onTrackPad)
{
//move character
}
break;
case MotionEvent.ACTION_DOWN:
// Fire bullets
break;
}
//The event was consumed
return true;
}
The problem is that I am not able to move and fire at the same time (I need to stop moving in order to fire and viceversa)
I am aware that Android can handle multi-touch events but have not figure it how to use that to be able to process these events and the same time so that the player can move and fire at the same time
I have also try using the getActionMasked without any luck
After reading this question Android MotionEvent.getActionIndex() and MultiTouch
This is how I solved the problem
public boolean onTouch(View v, MotionEvent event) {
int dest_x ;
int dest_y ;
p = event.getActionIndex() ;
dest_x = (int) event.getX(p);
dest_y = (int) event.getY(p);
onTrackPad = dbSettings.TRACK_PAD.contains(dest_x, dest_y);
action = event.getActionMasked() ;
switch (action) {
case MotionEvent.ACTION_MOVE:
if (onTrackPad)
{
//move character
}
break;
case MotionEvent.ACTION_DOWN:
// Fire bullets
break;
}
//The event was consumed
return true;
}
MotionEvent has all information about touches that you need. You can get number of touches by executing event.getPointersCount(), and try to check MotionEvent.ACTION_POINTER_2_DOWN instead of MotionEvent.ACTION_DOWN. To get coordinates of each touch you can use event.getX(0) and event.getX(1), same is for y. If you have a case of MotionEvent.ACTION_MOVE with 2 touches, you will receive all this information in your motion event.
Try below code.
when multiple pointers touches on the screen,system generates the action events.we can keep track of individual pointers with in motion event using pointer id. Pointer id persists across touch events and also allows to track individual pointer across entire gesture.
#Override
public boolean onTouch(View v, MotionEvent event) {
int index = event.getActionIndex();
int pointerID = event.getPointerId(index);
int action = event.getActionMasked();
if(event.getPointerCount() > 1)
{
Log.i("TouchType ", "Multi Touch");
for(int i = 0; i < event.getPointerCount(); i++)
{
performAction(action);
}
}else
{
Log.i("TouchType ", "Single Touch");
performAction(action);
}
return true;
}
public void performAction(int action){
switch(action)
{
case MotionEvent.ACTION_DOWN :
Log.i("OnTouch ", "Pressed");
// Fire bullets
break;
case MotionEvent.ACTION_MOVE :
Log.i("OnTiouch", "move");
//move character
break;
case MotionEvent.ACTION_UP :
Log.i("OnTiouch", "Up");
break;
default:
Log.i("OnTiouch", "None");
}
}
I've implemented onTouch for my layout to function as buttons.
It all works great besides the fact that a 2 clicks sound is made sometimes as touching the button.
I've tried to debug it but I wasn't able to understand what is the problem.
Here is the onTouch code :
button.setOnTouchListener(new OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
Log.d(TAG, "onTouch "+event.getAction());
switch (event.getAction())
{
case MotionEvent.ACTION_HOVER_ENTER:
v.setBackgroundResource(R.color.main_menu_buttons_bg_pressed);
break;
case MotionEvent.ACTION_HOVER_EXIT:
v.setBackgroundResource(R.color.background_color);
break;
case MotionEvent.ACTION_DOWN:
v.setBackgroundResource(R.color.main_menu_buttons_bg_pressed);
break;
case MotionEvent.ACTION_UP:
Log.d(TAG, "on click");
v.performClick();
v.setBackgroundResource(R.color.background_color);
break;
default:
// empty
}
return false;
}
});
As you can see I've added log messages, which usually prints :
onTouch 0
onTouch 2
onTouch 2
onTouch 2
onTouch 2
onTouch 1
on click
so it should be ok but yet the clicks are heard twice (sometimes, can't determine exactly when it happens)
it's the expected behaviour du of v.performClick(); , when the view has also an OnClikListener: From GrepCode
2480 public boolean More ...performClick() {
2481 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
2482
2483 if (mOnClickListener != null) {
2484 playSoundEffect(SoundEffectConstants.CLICK);
2485 mOnClickListener.onClick(this);
2486 return true;
2487 }
2488
2489 return false;
2490 }
I want to get activity or any control element's touch counts.
Like Android 4.2 on nexus Developer options Enable while touching 7 times.
What is the suitable event to handle it ?
You are getting 2 count's because the touch event are called every time a touch event is snet, like tump down and tump up (for example)
#Override
public boolean onTouchEvent(MotionEvent event) {
int action = MotionEventCompat.getActionMasked(event);
switch (action) {
case MotionEvent.ACTION_DOWN:
//add here the counter if you want when screen pressed
break;
case MotionEvent.ACTION_UP:
//add here the counter if you want when touch released
break;
default:
return super.onTouchEvent(event);
}
}
You can use a View.OnTouchListener, overriding the onTouch method :
#Override
public boolean onTouch(final View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// increment counter
break;
}
}
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;
}
});
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.. :)
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;
}
});
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);
}
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..
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);
}
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
i'am using imageview on touch listener in this motion event not working why this happen any one have idea regarding this help me.
here is my code
img_View11.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
Log.i("img_View11", "img_View11 _1");
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
Log.i("img_View11", "img_View11 _2");
break;
case MotionEvent.ACTION_MOVE:
Log.i("img_View11", "img_View11 _3");
break;
case MotionEvent.ACTION_UP:
Log.i("img_View11", "img_View11 _4");
break;
}
return false;
}
});
here my out put show like this img_View11_1 and two img_View11_2
Log.i("img_View11", "img_View11 _1");
Log.i("img_View11", "img_View11 _2");
Question: Why MotionEvent.ACTION_MOVE and MotionEvent.ACTION_UP not working ?
Change "return false;" to "return true;" so that it will continue to receive calls.