0

I have a button that I would like clickable only by a stylus. I used the method setClickable to enable or disable the click on the button, but how can I do that is clickable only with a pen? the button should be clickable only when MotionEvent.TOOL_TYPE_STYLUS

how do i can?

spyker10
  • 129
  • 2
  • 11

1 Answers1

0

you could override the Button's onTouchListener, and return immediately it the touch event is not performed through TOOL_TYPE_STYLUS. To retrieve this information, you can use

getToolType(int pointerX)

From the documentation

Gets the tool type of a pointer for the given pointer index

if it returns TOOL_TYPE_STYLUS, then you can simply check the MotionEvent for the ACTION_DOWN/ACTION_UP, and call performClick(). E.g.

b.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getToolType(0) == MotionEvent.TOOL_TYPE_STYLUS) {
                if (event.getAction() == MotionEvent.ACTION_UP ) {
                    performClick();
                    return true;
                }
            }
            return false;
        }
});
Blackbelt
  • 156,034
  • 29
  • 297
  • 305