Android accessibility identify heading
Asked Answered
B

4

11

I have the latest version of Talkback and its announcing "My Top level Text Heading". Android native behaviour is adding "Heading" for my top level elements. I could not find a way to switch ON/OFF heading announcement. Is there an API to control its behaviour. In the previous version of Talkback versions it was not announcing "Heading" by itself.

Blakney answered 24/10, 2016 at 5:42 Comment(2)
What kind of control are you using to get it to read this? I have the opposite problem, I WANT it to read heading and have been having to put it in manually on the content description.Anlace
@Anlace Were you able to solve your issue?Dareen
B
9

You can enable or disable the "heading" accessibility property of any View on API 19+:

ViewCompat.setAccessibilityDelegate(headingView, new AccessibilityDelegateCompat() {
  @Override
  public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCompat info) {
    super.onInitializeAccessibilityNodeInfo(host, info);
    info.setHeading(true); // false to mark a view as not a heading
  }
});

If you have minSdk 28, you can simply set this directly in your XML:

android:accessibilityHeading="false"
Belanger answered 7/1, 2019 at 21:51 Comment(3)
in which API this method added? AccessibilityNodeInfoCompat#setHeading(boolean)Vermillion
Hi @Adam S , this does not appear to work for me. The textview is not announcing that it is a heading when you do setHeading(true).Chenay
Yup, it's added in 28 developer.android.com/reference/android/view/accessibility/…Sohn
P
7

Here is a simpler method:

ViewCompat.setAccessibilityHeading(headingView, false);

https://developer.android.com/reference/androidx/core/view/ViewCompat#setAccessibilityHeading(android.view.View,%20boolean)

Partan answered 16/7, 2020 at 7:40 Comment(2)
It's for 19 and lower.Sohn
@KornilovRuslan The doc says that this is a no-op on API < 19Decagram
F
1

If you're only supporting API level 23 and above, you can simply do the following.

textView.setAccessibilityDelegate(new View.AccessibilityDelegate() {
  @Override
  public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
    super.onInitializeAccessibilityNodeInfo(host, info);
    //blanked to prevent talkback from announcing class/type
    info.setClassName("");
  }
});
Forsterite answered 13/9, 2017 at 21:11 Comment(0)
B
1

I solved this by passing heading as false in

AccessibilityNodeInfoCompat.CollectionItemInfoCompat.obtain method.

        // set the heading attribute to false so that heading is not announced in label  

info.setCollectionItemInfo(
    AccessibilityNodeInfoCompat.CollectionItemInfoCompat.obtain(glp.getSpanIndex(),
 glp.getSpanSize(), spanGroupIndex, 1, false, false));

public static CollectionItemInfoCompat obtain(int rowIndex, 
int rowSpan, int columnIndex, int columnSpan,
boolean heading, boolean selected)
Blakney answered 14/9, 2017 at 6:19 Comment(1)
how do you get glp, Or what's glp?Siloa

© 2022 - 2024 — McMap. All rights reserved.