Naveed
Naveed

Reputation: 3212

How to Announce for Accessibility for pre API 16?

The method announceForAccessibility(CharSequence text) was added in API 16 which lets you trigger an announcement whenever needed.

I tried using the accessibility manager to do the same in api level < 16 but it seems like the TYPE_ANNOUNCEMENT was also added in API 16

Is there a work around or a support method which would allow me to announce Accessibility for devices running api < 16?

Upvotes: 0

Views: 2918

Answers (2)

caseyburkhardt
caseyburkhardt

Reputation: 1195

Please avoid announceForAccessibility() or sending TYPE_ANNOUNCEMENT within applications unless you're conveying a message that is critical for use of the device. Speech from these events will be uninterruptible and may interfere with navigation or a user's interaction with TalkBack.

Upvotes: 2

Naveed
Naveed

Reputation: 3212

Adding this as answer by alanv in case that answer ever goes down. Originally posted here:

you will need to use a slight workaround and trigger an announcement by sending a VIEW_FOCUSED event on ICS, or use the announceForAccessibility API on JellyBean and above. Which would require the support-v4 library and would look like this:

/** The parent context. Used to obtain string resources. */
private final Context mContext;

/**
 * The accessibility manager for this context. This is used to check the
 * accessibility enabled state, as well as to send raw accessibility events.
 */
private final AccessibilityManager mA11yManager;

/**
 * Generates and dispatches an SDK-specific spoken announcement.
 * <p>
 * For backwards compatibility, we're constructing an event from scratch
 * using the appropriate event type. If your application only targets SDK
 * 16+, you can just call View.announceForAccessibility(CharSequence).
 * </p>
 *
 * @param text The text to announce.
 */
private void announceForAccessibilityCompat(CharSequence text) {
    if (!mA11yManager.isEnabled()) {
        return;
    }

    // Prior to SDK 16, announcements could only be made through FOCUSED
    // events. Jelly Bean (SDK 16) added support for speaking text verbatim
    // using the ANNOUNCEMENT event type.
    final int eventType;
    if (Build.VERSION.SDK_INT < 16) {
        eventType = AccessibilityEvent.TYPE_VIEW_FOCUSED;
    } else {
        eventType = AccessibilityEventCompat.TYPE_ANNOUNCEMENT;
    }

    // Construct an accessibility event with the minimum recommended
    // attributes. An event without a class name or package may be dropped.
    final AccessibilityEvent event = AccessibilityEvent.obtain(eventType);
    event.getText().add(text);
    event.setEnabled(isEnabled());
    event.setClassName(getClass().getName());
    event.setPackageName(mContext.getPackageName());

    // JellyBean MR1 requires a source view to set the window ID.
    final AccessibilityRecordCompat record = new AccessibilityRecordCompat(event);
    record.setSource(this);

    // Sends the event directly through the accessibility manager. If your
    // application only targets SDK 14+, you should just call
    // getParent().requestSendAccessibilityEvent(this, event);
    mA11yManager.sendAccessibilityEvent(event);
}

Upvotes: 2

Related Questions