7

I want Fragment to announce itself on launch in Android Talkback. Activity does announce itself when added 'android:label' on the activity tag in AndroidManifest file. How can I do that for Fragment?

Jonik
  • 80,077
  • 70
  • 264
  • 372
Yogesh Rathod
  • 111
  • 1
  • 8
  • I didn't known `android:label` gets announced automatically—thanks, just what I was looking for. – Jonik Oct 23 '17 at 13:08

2 Answers2

9

You can use one of the Fragment lifecycle methods to announce it yourself.

Fragments don't have an intrinsic title, and since they're basically Views + logic, it seems reasonable that the system can't predict when it would be appropriate to announce a newly added Fragment.

Something like overriding onCreateView(View view, ...) and then calling view.announceForAccessibility("title of my fragment") would work.

ataulm
  • 15,195
  • 7
  • 50
  • 92
  • Thing is, calling `announceForAccessibility` in `onCreateView` or `onViewCreated` does *not* seem to work. Similarly if you try it in `onResume` of an Activity: curiously, the label only gets announced when returning to my app, not when first opening the activity. – Jonik Oct 23 '17 at 13:21
  • @Jonik yes you're right, I've since seen this - the view must be attached to the window for this to work. I wonder if finding the `android.R.id.content` view and using that to announce would work at any point. – ataulm Oct 23 '17 at 19:44
2

I was having trouble with the same and the accepted answer did not work for me. I had a look at the lifecycle of a fragment and decided to call it on the last method before the fragment became active: onResume

@Override
public void onResume() {
    super.onResume();
    rootView.announceForAccessibility("title of my fragment");
}
Shameed
  • 33
  • 5