Pronounce custom statement instead of checked/unchecked

Viewed 1667

I am looking to see if it's possible to change the checked/unchecked statement made during TalkBack for radio buttons in Android. When the radio button is currently in focus, TalkBack will say whether it is checked or unchecked. I wanted to see if it is possible to change it to say "selected" and "not selected".

With all the flexibility that Android has in controlling this I was just wanting to see if it can be done or not, and, if so, how to do it.

2 Answers

As can be seen in the sources, RadioButton declares itself to be a checkable accessibility node. What you can do, is reset that and mark it as selected:

    ViewCompat.setAccessibilityDelegate(radioButton, new AccessibilityDelegateCompat() {
        @Override
        public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCompat info) {
            super.onInitializeAccessibilityNodeInfo(host, info);
            info.setCheckable(false); // Checked/unchecked won't be pronounced
            info.setSelected(radioButton.isChecked()); // if item is selected, then "selected" will be pronounced
        }
    });

A slightly updated version of this answer in Kotlin.

override fun onInitializeAccessibilityEvent(event: AccessibilityEvent?) {
    super.onInitializeAccessibilityEvent(event)
    event?.isChecked = radioButton.isChecked
}

override fun onInitializeAccessibilityNodeInfo(info: AccessibilityNodeInfo?) {
    super.onInitializeAccessibilityNodeInfo(info)
    info?.isCheckable = true
    info?.isChecked = radioButton.isChecked
}
Related