Reputation: 2729
If I have two buttons in my view and I want to enable OntouchListener for that two buttons at the same time,I mean touching 1st button started one task and holding that 1st button touching 2nd button started another task. How to do it?Waiting for help.
Upvotes: 0
Views: 2440
Reputation: 869
it is quite simple
@Override
public boolean onTouch(View v, MotionEvent arg1)
{
switch (v.getId()) {
case R.id.button1:
if(arg1.getAction()==MotionEvent.ACTION_DOWN)
{
//your event
}
break;
case R.id.button2:
if(arg1.getAction()==MotionEvent.ACTION_DOWN)
{
//your event
}
break;
default:
break;
}
return true;
}
Upvotes: 0
Reputation: 2896
Try the following code snippet.
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
//first finger went down
break;
case MotionEvent.ACTION_MOVE:
//a touch placement has changed
break;
case MotionEvent.ACTION_UP:
//first finger went up
break;
case MotionEvent.ACTION_CANCEL:
//gesture aborted (I think this means the finger was dragged outside of the touchscreen)
break;
case MotionEvent.ACTION_POINTER_DOWN:
if(mFirstButton.isPressed() && mSecondButton.isPressed())
{// your code.}
break;
Upvotes: 3
Reputation: 364
So I am not sure what you want to accomplish, but you have something like:
private boolean mFirstButtonDown = false;
@Override
public boolean onTouchEvent(MotionEvent event){
// first button onTouchEvent
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
mFirstButtonDown = true;
break;
case MotionEvent.ACTION_UP:
mFirstButtonDown = false;
break;
}
....
}
For the second button just check if it is still holded:
@Override
public boolean onTouchEvent(MotionEvent event){
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
if (mFirstButtonDown && (event.getPointerCount() > 1)) {
// do task
}
}
To simultaneously touch 2 buttons use multi-pointers (or multi-touch), you can find more details on this post: Multiple button presses for Android 2.x
Another good article that can help you understand multi-pointers is on Android developers: http://android-developers.blogspot.ro/2010/06/making-sense-of-multitouch.html
The code above is not tested, it should just give you an idea:)
Upvotes: 2
Reputation: 4307
You can either add two separate OnTouchListener
s to the two buttons, and indicate the touched state of the buttons with two booleans in your Activity
, or you can add a single OnTouchListener
to your parent View
and compare the touch coordinates of the pointers in the MotionEvent
to the two Buttons' coordinates.
Upvotes: 0
Reputation: 6588
You can add a onTouchListener to that view which will handle multiple touch.
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
//first finger went down
break;
case MotionEvent.ACTION_MOVE:
//a touch placement has changed
break;
case MotionEvent.ACTION_UP:
//first finger went up
break;
case MotionEvent.ACTION_CANCEL:
//gesture aborted (I think this means the finger was dragged outside of the touchscreen)
break;
case MotionEvent.ACTION_POINTER_DOWN:
//second finger (or third, or more) went down.
break;
case MotionEvent.ACTION_POINTER_UP:
//second finger (or more) went up.
break;
default: break;
}
return true;
}
Check if your button is clicked inside respective cases.
Upvotes: 6