How to slide to perform clicks android - android

I'm trying to make an application that the user can slide or drag the finger across a set of buttons to perform a click. I've tried onTouch with MotionEvent.ACTION_MOVE but I can't seem to get it to work. The buttons just don't press at all. Here's the code :
public boolean onTouch(View v, MotionEvent arg1) {
if (arg1.getAction() == MotionEvent.ACTION_MOVE) {
switch (v.getId()) {
case R.id.key1:
key1.performClick();
break;
case R.id.key2:
key2.performClick();
break;
case R.id.key3:
key3.performClick();
break;
case R.id.key4:
key4.performClick();
break;
case R.id.key5:
key5.performClick();
break;
case R.id.key6:
key6.performClick();
break;
case R.id.key7:
key7.performClick();
break;
case R.id.key8:
key8.performClick();
break;
}
}
return true;
}

I think you are better off capturing the key events on all those buttons and then performing some sort of sum on each one. For example
Key1 = 1
key2 = 1+2
key3 = 1+2+3
...
When you reach keyx, you can check if the sum matches your expectation. If it does, the user probably swiped his / her finger across the buttons. Use some sort of timing by which keyx must be pressed and the numbers must add up to N. That will avoid the user pressing button3 and then coming back to button1 etc and still registering a click.
If you want to register a click on each button, set their onclick listeners and ensure that the buttons themselves are clickable (they should already be).

Related

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)

Select other buttons on touch move

I'm writing a test which options are placed in buttons. I have a selector for buttons that displays normal and pressed state and everything works great.
What i want is that when user touches an option and moves to next option, the next button get focus and select. And when user takes the hand, perform the selected button click.
In images above you can see exactly what i mean.
You need to write a custom touch event management that will trigger setPressed(true) on MotionEvent.ACTION_DOWN OR MotionEvent.ACTION_MOVE (the default implementation expects to always receive MotionEvent.ACTION_DOWN).
Code like this should work (add it to all your buttons):
button.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
if (!v.isPressed()) {
v.setPressed(true);
}
return true;
case MotionEvent.ACTION_UP:
// perform action ON CLICK
break;
}
v.setPressed(false);
return false;
}
});

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;
}

Detect when a user moves off of a button in 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

On pressed button block the other buttons

How could I make my button to do the on click action even if the button is pressed and the finger is still on the screen (the button is not released). Or I would accept the solution that when a button is pressed and is not release, the other buttons on the layout to not be blocked.
Instead of using an OnClickListener consider using an OnTouchListener. You can then detect when the user's finger touches:ACTION_DOWN and releases ACTION_UP the button.
A method like this would probably work fine:
public boolean onTouch(View v, MotionEvent event)
{
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
//DISABLE THE OTHER BUTTONS
break;
case MotionEvent.ACTION_UP:
// RE-ENABLE THE OTHER BUTTONS
break;
}
return true;
}
One option would be to use the onmousedown event rather than the onclick event in your javascript:
myButton.onmousedown = myOnClickFunction;

Categories

Resources