9

I am new with android accessibility - TalkBack. I am aware of basic things like contentDescription, importantForAaccessibility and how they make node tree, etc.

In my problem, I want to shift accessibility focus on RecyclerView's first element on some action.

Usually someView.requestFocus() or someView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED) works. But it seems to not working as expected in RecyclerView.

I have tried to solve it by:

  1. android:accessibilityLiveRegion="polite" but it only announce that list is there. But it doesn't go on an element with accessibility focus.

  2. By focusing the first element from the adapter - but it's a bad idea to do so!

Other than that, android:accessibilityTraversalAfter and android:accessibilityTraversalBefore also don't work well with a list view in android.

someView.requestFocus()
someView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED)
Diego Malone
  • 1,044
  • 9
  • 25
Dhara Vamja
  • 554
  • 4
  • 17
  • 2
    In general, it isn't a good idea to set focus; that is something that is best left to accessibility service like Talkback to figure out. If focus is assigned on an app-specific basis, the experience for the user of accessibility services becomes fragmented. – Shailen Tuli Jun 03 '19 at 15:17
  • In my case, While the user visits a screen, API call is made. Once It gives a response, Recycler View will appear. When this view is being visible, It should focus on that list. I think it would be easy for users if accessibility focus detects this change. – Dhara Vamja Jun 04 '19 at 09:41
  • @DharaVamja Hey sorry if I write you here but I seen you answered a lot of questions about accessibility and I would like to ask you a question too. Do you know how to have a listener for accessibility focus in Android? Something that, when a button, label or else (a View in general) become focused by the screen reader, I can add a behavior to (like simply have a log saying "I am focused")? Thanks – Poli97 Jan 24 '20 at 15:29
  • @Poli97 Hey, sorry for late replay, Yes, I have read something about it when doing R & D on it, but haven't tried it as I didn't get any use case for the same. If you are facing issues in a nested scroll view or simple scroll view - jump child view problem, then there's a solution. – Dhara Vamja Feb 03 '20 at 06:33

2 Answers2

7

RecyclerView's getChildAt() method worked for me.

recyclerView.getChildAt(0).requestFocus()
recyclerView.getChildAt(0).sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED)
Dhara Vamja
  • 554
  • 4
  • 17
2

Request Focus with Specific ViewId and gets the first element of that node.

mini developer
  • 302
  • 3
  • 7
  • 1
    I think `viewId` won't work with accessibility focus shift. We will need `View` itself to get focused. Thank you for your answer it helped me to get a solution. :) – Dhara Vamja Aug 06 '19 at 08:26