So I have special question to you:
How can I catch buttons when I use move action? How can I handle moves on my screen for buttons(or other elements)?
I used MotionEvent (ACTION_MOVE) but by using that fragment of code i don't get the desired result
in OnCreate
btn1.setOnTouchListener(this); //for all of buttons on Activity
in onTouch
switch(event.getAction()) {
case MotionEvent.ACTION_MOVE:
//actions
break;
}
actions will be occur for first button from which the movement starts. In other case nothing will occur
At this time I think that I can use ACTION_MOVE for my Activity not for each button or other elements and save coordinates of buttons(top left and bottom right) to arraylist.
And when movement starts I could compare this coordinates and real coordinates of movement.
So by that way I could know on which buttons movement was.
Probably I reinvent the wheel. That why I ask for your help)
Would you explain more clear? From what I see you need probably do two things:
First add listener to all buttons and let your activity implement onTouchListener.
Second you need to save initial coordinate for each button in MotionEvent.ACTION_DOWN(if I'm right about syntax).
This way you will have access to all buttons of yours and you can save their coordinates correctly
You will get the view inside the touch listener, using which you can identify.
Button btn1 = (Button) findViewById(R.id.btn1);
Button btn2 = (Button) findViewById(R.id.btn2);
Button btn3 = (Button) findViewById(R.id.btn3);
btn1.setOnTouchListener(touchListener);
btn2.setOnTouchListener(touchListener);
btn3.setOnTouchListener(touchListener);
private OnTouchListener touchListener = new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
switch (v.getId()) {
case R.id.btn1:
//Button 1
break;
case R.id.btn2:
//Button 2
break;
case R.id.btn3:
//Button 3
break;
}
}
return false;
}
};
Related
I have a RelativeLayout with a single ImageView inside of it. The ImageView has an OnTouchListener attached to it which is working perfectly fine if I just press the ImageView. The problem I'm having though is that if I press and hold in the empty space outside of the ImageView and then try to press the ImageView the touch listener doesn't fire. It seems like the layout is absorbing the touch events somehow. Any ideas on how I could fix this?
The action defined by you here is a Multi-touch gesture. Go through this doc to know how to handle this type of actions.
Also take a look at the ACTION_POINTER_DOWN action define in MotionEvent. This is the event that will get called when additional fingers come down.
you mout use this code:
imageView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
//your code
break;
case MotionEvent.ACTION_UP:
//your code
break;
case MotionEvent.ACTION_MOVE:
//your code
break;
default:
//your code
break;
}
return true;
}
});
I have several buttons and I would like to press on one of them and drag through another presing them. Could you tell me which MotionEvent or another functionality should I use. I'm using onTouchListener.
There is an image where you can see what I want to do (first ACTION_DOWN on 1st button and drag through 2nd-7th buttons still pressing the screen) and finally press every white buttons:![enter image description here][1]
http://i.stack.imgur.com/QNfvT.png
Below is my onTouch button code:
button1 = (Button) findViewById(R.id.button1);
button1.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
soundIDs[0] = sound.play(R.raw.sample1);
button1.setBackgroundResource(R.drawable.white_clicked);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
sound.stop(soundIDs[0]);
button1.setBackgroundResource(R.drawable.white);
break;
}
return false;
}
});
I have several buttons and I would like to press on one of them and drag through another presing them. Could you tell me which MotionEvent or another functionality should I use. I'm using onTouchListener.
There is an image where you can see what I want to do (first ACTION_DOWN on 1st button and drag through 2nd-7th buttons still pressing the screen) and finally press every white buttons:
Below is my onTouch button code:
button1 = (Button) findViewById(R.id.button1);
button1.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
soundIDs[0] = sound.play(R.raw.sample1);
button1.setBackgroundResource(R.drawable.white_clicked);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
sound.stop(soundIDs[0]);
button1.setBackgroundResource(R.drawable.white);
break;
}
return false;
}
});
You are setting the OnTouchListener on just the one button. That's not going to help you know when the pointer moves (e.g. user drags his finger) into another button.
You could set an OnTouchListener on the view that contains the buttons. Then check for ACTION_DOWN, ACTION_MOVE, and ACTION_UP events. You would then have to do some simple hit detection to figure out which button to activate.
Something along the lines of:
getWindow().getDecorView()
.findViewById(android.R.id.content)
.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent event) {
int action = event.getAction();
if (action != MotionEvent.ACTION_DOWN
&& action != MotionEvent.ACTION_MOVE
&& action != MotionEvent.ACTION_UP) return false;
Rect hitRect = new Rect();
Button button;
for(int i = 0; i < myButtons.size(); i++) {
button = myButtons.get(i);
button.getHitRect(hitRect);
if (hitRect.contains((int)event.getX(), (int)event.getY())) {
button.setText("hit!");
}
}
return true;
}
});
Where myButtons is an ArrayList of your buttons (piano keys in your example).
Also, you'd probably want to modify this to properly deactivate the currently active button if the user's touch leaves the button, but doesn't hit another button.
I tested the above code on an android device with a layout that has 3 buttons in a row. Dragging your finger across all the buttons causes each button's text to change to "hit!"
Like I said above, you were setting the touch listener on the just one button, that will not work. In this example I have set the touch listener on the entire view for the activity.
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).
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;