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.
5 Answers
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.

- 6,548
- 5
- 43
- 69
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;
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:)
-
Once one button gets hold another doesn't work.How a Boolean flag can solve this problem ? – Mamun Sardar Jul 24 '12 at 13:21
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.

- 4,287
- 1
- 23
- 25
-
But I've already two separate OnTouchListener for that two buttons. Once one button gets hold another doesn't work.How a Boolean flag can solve this problem ? – Mamun Sardar Jul 24 '12 at 13:19
-
Add `button1.isPressed()` inside the `OnTouchListener` of your second button to check if first button is pressed. – JiTHiN Jul 25 '12 at 03:17
-
@MamunSardar I am facing the same issue were you able to resolve it?. When one part of my screen is pressed there are no touch events registered after that. – Vishwesh Shetty Feb 23 '13 at 18:20
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;
}

- 869
- 1
- 5
- 5