Search code examples
androidaccessibilityandroid-viewtalkback

How can I maintain a one pointer gesture when explore-by-touch is enabled?


I have a custom view that captures the user's signature (John Hancock). I want our application to be as accessible as possible, so I'm taking special care to ensure to optimize our screens for TalkBack and Explore-by-Touch. Since Explore-by-Touch changes all one finger gestures into two finger gestures, it breaks the custom signature view.

What I'd like to do is have Explore-by-Touch announce the view's content description on hover, then enable the view when the user double-taps. This will allow them to draw on top of the view with a single pointer like a normal user.

I've searched around but it's difficult to find detailed documentation on the Android accessibility libraries. Any ideas?


Solution

  • When Explore by Touch is turned on, single-finger touch events are converted into hover events. You can watch these events by adding an OnHoverListener to your view or overriding View.onHoverEvent.

    Once you're intercepting these events, you can usually just pass them to your normal touch handling code and map from hover actions to touch actions (as below).

    @Override
    public boolean onHoverEvent(MotionEvent event) {
        if (mAccessibilityManager.isTouchExplorationEnabled()) {
            return onTouchEvent(event);
        } else {
            return super.onHoverEvent(event);
        }
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_HOVER_ENTER:
                return handleDown(event);
            case MotionEvent.ACTION_MOVE:
            case MotionEvent.ACTION_HOVER_MOVE:
                return handleMove(event);
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_HOVER_EXIT:
                return handleUp(event);
        }
    
        return false;
    }