Android - Enter button with finger detection? - android

I'm working on a soundboard, however I've got a problem when it come to drag the finger over the screen to play the sounds for the buttons I drag the finger over.
Button Button3 = (Button)findViewById(R.id.button03);
Button3.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
mp.playSound(3);
}
return false;
}
});
Do anyone know how I can detect when a finger enter a button and not click the button?
Thanks :)

Look into onTouchEvent
public boolean onTouchEvent (MotionEvent event)
Since: API Level 1
Implement this method to handle touch screen motion events.
Parameters
event - The motion event.
Returns
True - if the event was handled, false otherwise.

Related

Why does an onTouchListener not report MotionEvent.ACTION_UP until an onClickListener is specified?

I have discovered some weird functionality when using android...
If I specify a ConstraintLayout's onTouchListener like so:
ConstraintLayout clSend = fragment.findViewById(R.id.alert_message_send_layout);
clSend.setOnTouchListener((view, event) -> {
Log.d("onTouch event: %d", event);
return view.onTouchEvent(event);
});
Then I press the layout, I get the result:
onTouch event: 0 // MotionEvent.ACTION_DOWN
Which is expected, however when holding on the press and releasing after a short period (2-3 seconds), onTouch() is only called for the ACTION_DOWN event. I would expect there to be a MotionEvent.ACTION_UP event but no...
But it gets weirder...
If I repeat the above but add an onClickListener like so:
ConstraintLayout clSend = fragment.findViewById(R.id.alert_message_send_layout);
clSend.setOnClickListener(v -> {});
clSend.setOnTouchListener((view, event) -> {
Log.d("onTouch event: %d", event);
return view.onTouchEvent(event);
});
Then I get both an ACTION_DOWN event, and an ACTION_UP event.
Does anyone have an explanation for this?
I've checked the Documentation:
View.OnTouchListener and
MotionEvent
I've checked the android issue tracker
I've checked here on StackOverflow.com but nothing explains it...
"One final search to make sure I'm not being lazy..."
#Danpe answers it perfectly:
MotionEvent.ACTION_UP Won't get called until the MotionEvent.ACTION_DOWN occurred, a logical explanation for this is that it's impossible for an ACTION_UP to occur if an ACTION_DOWN never occurred before it.
So all that has to be done is:
...
#Override
public boolean onTouch(View v, MotionEvent event) {
...
if (event == MotionEvent.ACTION_DOWN) {
return true;
}
...
}

Android: Disable touch after double tap or long press

I have created a GestureDetector to detect a double tap or long press. If either of them are triggered, I would like to disable any more touches. How can I do this?
Thanks!
Well there could be two ways to do this.
1. you have to implement onTouchListener along with a conditional variable say boolean. declare
Boolean isDouble = false; // your activity level variable
now when you have detected the double touch or longpress set it to true
isDouble = true;
now in your onTouchListener .
view.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(isDouble)
return false;
}
});
Other method may not work properly on some devices but it can work for your case.
view.setOnTouchListener(null);

Android remove onInterceptTouchEvent when tapping

Hello right now I'm developing a custom view where there'll be 2 listview that the header can be stacked each other like this picture :
||
||
\/
I already succeed to create this view, by overriding overscrolled and intercept all touch and playing with the margin of the header contents. but the problem now is because I intercept all touch now I can't tap the content of the listview, I know the flow of the touch is from ACTION_DOWN -> ACTION_MOVE -> ACTION_UP, and then when I dispatch the event to the child the tap is working but the scroll isn't working at all.
So for now the flow for my view is like this onInterceptTouchEvent -> onTouch -> gestureListener (or with overscroll)
this is some part of my code
#Override
public boolean onInterceptTouchEvent(MotionEvent ev)
{
onTouchEvent(ev);
return true;
}
#Override
public boolean onTouch(View v, MotionEvent event) {
topListViewHeight = lvFirstListView.getTotalHeight() - rlFirstHeader.getHeight();
if(isBottomScrollable) {
listGestureDetector.onTouchEvent(event);
lvSecondListView.dispatchTouchEvent(event);
} else {
gestureDetector.onTouchEvent(event);
}
return true;
}
Is there anybody here have an idea about this? Any suggestion is very appreciated and if you need more question about this question please ask me.
Thanks before

How can I know the view is in touch state in Android

How can I know the view is in touch state.
If more than one touch points on one view,How can I catch the event of the last up touch point.
Please help?
You can override onTouchEvent() on your View. ACTION_DOWN will be given when the first "pointer" is placed. From then on, you will get ACTION_POINTER_DOWN or ACTION_POINTER_UP as subsequent fingers are pressed down and then released. Then, when the last pointer/finger is released, your View will get ACTION_UP. This is spelled out clearly in the MotionEvent docs.
Something like this might be what you're looking for, just subclass whatever View you are working with.
#Override
public boolean onTouchEvent(MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN)
isTouching = true;
else if(event.getAction() == MotionEvent.ACTION_UP)
isTouching = false;
return super.onTouchEvent(event);
}

Android - Button OnTouch event change focus of Button

I have an App with 10 Buttons. Everytime the User presses on one Button A TextView should change. And if the User changes the focus and ,oves its finger to right, to the next button(without taking the finger off the screen) the seond button should be focused.
I tried it with setting an OnTouchListner to all buttons, but once the finger is moved the focus still stays on the first button:
btn1.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
txt1.settext("1");
return false;
}
});
I hope you understood what I mean and can help me with this.
Thanks
Edit:
I found an Application which does that, here is a Video, so you can visualise what I mean.
Notice how I move the mouse(the finger in this case) and the Boxes change its focus:
http://www.youtube.com/watch?v=MRVFpNrBmsA&feature=youtu.be
To start in your onTouch() you should handle the events...
Something like
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_HOVER_ENTER) {
txt1.settext("1");
} else if(event.getAction() == MotionEvent.ACTION_HOVER_EXIT){
txt1.settext("0");
}
return true;
}
Not sure if those are the proper actions to check against but it is just to give the idea.
Also I believe you should return true otherwise it means that the event wasn't processed and it gets stuck.

Categories

Resources