Jason Hu
Jason Hu

Reputation: 1267

Android accessibility talkback to say fragment's title

Is there a way to get the talkback function in Android accessibility to say something after a fragment transaction? I basically want the talkback to say the name/title of the fragment after switching. These titles are set as the titles of the action bar, can they be accessed there? A user can move their finger on top of the text in the action bar to know what screen they are on, but the user won't know that unless they were familiar with the app already.

Upvotes: 8

Views: 9529

Answers (3)

BladeCoder
BladeCoder

Reputation: 12949

The recommended way since Android 9 is to set an accessibility pane title on the root view of your Fragment. Here's the backwards-compatible way which works on API 19+:

ViewCompat.setAccessibilityPaneTitle(view, "My fragment title")

If you're using AppCompat, you can also provide the title in your XML layout in a backwards-compatible way:

android:accessibilityPaneTitle="@string/fragment_title"

The title will be announced automatically when the new fragment is swapped in.

For more information read the official Android documentation for accessibility pane titles.

Upvotes: 3

Amit
Amit

Reputation: 3712

You can also use

binding.rootView.announceForAccessibility("My fragment title")

in onViewCreated() function of your fragment.

If you are not using viewBinding then just use the root view of the fragment's layout to call the function announceForAccessibility()

Upvotes: 0

alanv
alanv

Reputation: 24124

You can force a WINDOW_STATE_CHANGED event from the decor view to announce the ActionBar title. This will also cause TalkBack to clear accessibility focus, so you should only call it when the app's primary content changes.

getWindow().getDecorView()
        .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);

Upvotes: 9

Related Questions