Skip to content
This repository has been archived by the owner on Feb 11, 2022. It is now read-only.
Ataul Munim edited this page May 2, 2018 · 6 revisions

Determine if an accessibility service is running

  • Check if TalkBack (or other spoken feedback accessibility service) is enabled
  • Reports as enabled even if TalkBack is suspended
AccessibilityServices services = AccessibilityServices.newInstance(context);
services.isSpokenFeedbackEnabled();

There's other methods also:

  • isClosedCaptioningEnabled()
  • isSwitchAccessEnabled()

Add custom usage hints

Usage hints are read aloud by TalkBack (and made available to other accessibility services). On TalkBack, they're read aloud after the content description for a view. For example, "Log in button... Double tap to activate", where "Double tap to activate" is the usage hint.

These can be customized on Lollipop and above:

UsageHintsAccessibilityDelegate usageHintsDelegate = new UsageHintsAccessibilityDelegate(getResources())
usageHintsDelegate.setClickLabel("sign in");
ViewCompat.setAccessibilityDelegate(logInButton, usageHintsDelegate);

so now it'll say, "Log in button... Double tap to sign in".

Create custom accessibility actions

  • a wrapper around View actions to facilitate actions via dialog or TalkBack's local gestures menu.

There's two ways to create custom actions. The recommended approach is to use menu resources.

// res/menu/tweet_actions.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@id/tweet_action_open"
        android:title="@string/tweet_action_open" />

    <item
        android:id="@id/tweet_action_reply"
        android:title="@string/tweet_action_reply" />

    <item
        android:id="@id/tweet_action_retweet"
        android:title="@string/tweet_action_retweet" />
</menu>

With a set of defined actions in a menu resource (IDs and titles required), inflate a Menu with a ActionsMenuInflater:

    MenuItem.MenuItemClickListener menuItemClickListener = createMenuItemClickListener();
    Menu actionsMenu = ActionsMenuInflater.from(view.getContext())
            .inflate(R.menu.tweet_actions, menuItemClickListener);
    UsageHints usageHints = new UsageHints(view.getResources());
    usageHints.setClickLabel(R.string.tweet_actions_usage_hint);
    AccessibilityDelegateCompat delegate = new ActionsMenuAccessibilityDelegate(menu, menuItemClickListener, usageHints);
    ViewCompat.setAccessibilityDelegate(view, delegate);
}

private MenuItem.OnMenuItemClickListener createMenuItemClickListener() {
    return (MenuItem item) -> {
        switch (item.getItemId()) {
            case R.id.tweet_action_open:
                // do something
                return true;
                ...
        }
    };
}

You can use the same Menu to display a dialog that lists all the actions:

if (services.isSpokenFeedbackEnabled()) {
    AlertDialog alertDialog = ActionsMenuAlertDialog.create(view.getContext(), menu, menuItemClickListener).create();
    view.setClickListenerToShow(alertDialog);
} else {
    view.setIndividualClickListeners(tweet, listener);
}