Detect when a user moves off of a button in Android - android

Is it possible to detect anytime a user moves their finger off of a button after they have been pressing down on it?
I have determined that onTouchListener will get an action when the user moves off of the button vertically (It gives an action: ACTION_CANCEL), but I have not been able to find a way to determine when a user moves off the button horizontally.
public boolean onTouch(View arg0, MotionEvent arg1) {
switch(arg1.getAction()){
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
switch(arg0.getId()){
case R.id.my_button:
button.setBackgroundResource(R.drawable.untouched);
break;
}
break;
case MotionEvent.ACTION_DOWN:
switch(arg0.getId()){
case R.id.my_button:
button.setBackgroundResource(R.drawable.touched);
break;
}
break;
}
return false;
}

Try go next:
arg1.getX();
arg1.getY();
arg1 - is from your code
Here you can get coordinates of MotionEvent. Next you will do anything with this coordinates
Hope, it help you

Related

How to record (x,y) of a gesture at a fixed frequency?

I am now trying to record some (x,y) of a gesture. Now I have referred to some material and found that OnTouch function can help us get the (x,y) of the point that we touch down. However, I now need to move my finger and record the (x,y) of the points in the path at a fixed rate. How can I do that?
Try with the following code it will help you.
#Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//invoke when you will touch down finger on screen
break;
case MotionEvent.ACTION_MOVE:
//invoke when you will move finger on screen
fingerMove(event.getX(), event.getY());
break;
case MotionEvent.ACTION_UP:
//invoke when you will up finger from screen
break;
}
return true;
}

Android - multiple finger touches for 1 button

I want a button that can detect multiple fingers. Like ı am holding with 1 finger and then 1 more finger touches that button it has to detect that.
Or
a button that when pushed, automatically release that button dont care if it actually released or not.
I use onTouchevent for multiple buttons for my drum kit project.
#Override
public boolean onTouch(View view, MotionEvent motionEvent)
{
Animation zoomIn = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.zoomin);
switch (motionEvent.getAction())
{
case MotionEvent.ACTION_DOWN:
switch(view.getId())
{
case R.id.BassL:
findViewById(R.id.bass).startAnimation(zoomIn);
sp.play(BassD,1.0f,1.0f,0,0,1.0f);
break;
case R.id.BassR:
findViewById(R.id.bass).startAnimation(zoomIn);
sp.play(BassD,1.0f,1.0f,0,0,1.0f);
break;
case R.id.SnareL:
findViewById(R.id.snare).startAnimation(zoomIn);
sp.play(SnareD,1.0f,1.0f,0,0,1.0f);
break;
case R.id.SnareR:
findViewById(R.id.snare).startAnimation(zoomIn);
sp.play(SnareD,1.0f,1.0f,0,0,1.0f);
break;
case R.id.OpenHihat:
findViewById(R.id.hihat).startAnimation(zoomIn);
sp.stop(OpenH);
sp.play(OpenH,1.0f,1.0f,0,0,1.0f);
break;
case R.id.CloseHihat:
findViewById(R.id.hihat).startAnimation(zoomIn);
sp.play(CloseH,1.0f,1.0f,0,0,1.0f);
break;
case R.id.RideSurface:
findViewById(R.id.ride).startAnimation(zoomIn);
sp.stop(RideS);
sp.play(RideS,1.0f,1.0f,0,0,1.0f);
break;
case R.id.RideTop:
findViewById(R.id.ride).startAnimation(zoomIn);
sp.play(RideT,1.0f,1.0f,0,0,1.0f);
break;
}
}
return true;
}
}
You can check the pointer id associated to the MotionEvent you've just received.
See the docs for more info:
https://developer.android.com/reference/android/view/MotionEvent.html#getPointerId(int)

Android Listener to detect when the screen is being pressed

I have a view in my Android app that should be updated constantly when the screen is being pressed. Most listeners I found only invalidate the view once when the user touches the screen for the first time, or when the user removes his hand from the screen.
Is there such a thing as a listener that is constantly triggered as long as the screen is touched ? Or is there any other way to do this ? I know, that sounds like something really simple, but I haven't found a way to do it.
Override onTouch() and set a flag in ACTION_DOWN. As long as ACTION_UP isn't called after that, the user is touching the screen.
boolean pressed = false;
#Override
public boolean onTouch(View v, MotionEvent event) {
//int action = event.getAction();
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
pressed = true;
break;
case MotionEvent.ACTION_MOVE:
//User is moving around on the screen
break;
case MotionEvent.ACTION_UP:
pressed = false;
break;
}
return pressed;
}

Android Distinguishing Between Click and Drag dispatchTouchEvent

I am currently using dispatchTouchEvent to grab touch events, is there an easy way to distinguish between a click and a "drag" stylt gesture?
DispatchTouchEvent is called with a MotionEvent parameter. Method getAction within MotionEvent can return
ACTION_DOWN
ACTION_MOVE
ACTION_UP
ACTION_CANCEL
Then set on ACTION_DOWN flag isClick. If there is ACTION_MOVE clear isClick flag.
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
isClick = true;
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
if (isClick) {
//TODO Click action
}
break;
case MotionEvent.ACTION_MOVE:
isClick = false;
break;
default:
break;
}
return true;
}
set up a threshold limit.When you move the pointer within a small range make it recognized as a click or else a movement

Multitouch issue: when pressing two buttons at the same time, it detects as if I touched only one with my fingers, why?

So I have two buttons and would like to press both at the same time. If I press the first it logs a "C" (as a piano note), the other logs a D.
So far:
#Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getActionMasked();
switch (event.getAction() & MotionEvent.ACTION_MASK){
case MotionEvent.ACTION_DOWN:
switch (v.getId()) {
case R.id.cnotebutton:
Log.i("C", "C1");
return true;
case R.id.c2notebutton:
Log.i("D", "D1");
return true;
default:
}
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_POINTER_UP:
switch (v.getId()) {
case R.id.cnotebutton:
Log.i("C", "C2");
return true;
case R.id.c2notebutton:
Log.i("D", "D2");
return true;
default:
}
break;
case MotionEvent.ACTION_MOVE:
break;
default:
}
return true;
}
I Log C1 and C2 to distinguish the cases, but every time I press the buttons I get only C1,C2 or D1,D2 as if I touched the same buttons with both of my fingers. I should get C1,D2 or D1,C2 depending on which finger pointed first. Any suggestions?
Anyway I havent found a piano sample project yet, but that would definetly help me, if this is not going to work or my approach is totally wrong. Is there any?
Thanks in advance!
I think public boolean onInterceptTouchEvent (MotionEvent ev)is what you are looking for. Take a look at this.

Categories

Resources