I am trying to identify UI-control that fired MotionEvent in Android. I have one doubleTapDetector
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
doubleTapDetector = new GestureDetector(this, new DoubleTapDetector());
}
declared as
private class DoubleTapDetector extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onDoubleTap(MotionEvent e) {
String uiControlName = obtainUiControlName(e);
// Do something depends on uiControlName
return true;
}
private String obtainUiControlName(MotionEvent e) {
int deviceId = e.getDeviceId();
switch (deviceId) {
case R.id.button1: return "Button1";
case R.id.button2: return "Button2";
}
return null;
}
}
placed on both buttons
Button button1 = (Button) findViewById(R.id.button1);
outcomeButton.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
doubleTapDetector.onTouchEvent(event);
return true;
}
});
Button button2 = (Button) findViewById(R.id.button2);
outcomeButton.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
doubleTapDetector.onTouchEvent(event);
return true;
}
});
The problem is that deviceId always equals 0 and I can’t identify which button fires double click event. Is there a way to do that without implementing of two different doubleTapDetector’s for each button?
Mistake. getDeviceId returns physical pointer device id from where touch was received. Not Widget ID.
I feel that only way to do it is to create unique DoubleTapDetector for each your button, and also store some kind of button’s ID in your DoubleTapDetector class. Because onDoubleTap method doesn’t give enough info about Widget on which tap happened.
Like this: