3

I have an activity that has a fragment. That fragment gets the accessibility focus correctly. But when a replace the first fragment with another one, the second one is not getting the focuse automatically, I have to touch the fragment to get the focus.

This is the way a do the replace:

val fragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.container, myFragment)
fragmentTransaction.commit()
container.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED)

I try to add the android:importantForAccessibility and the android:clickable="true" to the second fragment (the one that is not getting accessibility focus)

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical"
    android:clickable="true"
    android:focusable="true"
    android:importantForAccessibility="yes">

    ...

</LinearLayout>

I also try to use the add fragment instead of replace with the same result.

Thanks!

esteban
  • 543
  • 4
  • 18

3 Answers3

2

An alternate solution - you can simply notify a user about screen change. If your fragment contains a toolbar, then you can set it focused while fragment is being displayed. or you could focus on the first/main reasonable element. In this way, It will announce a new screen to the user and it will be more helpful to users.

toolbar.requestFocus()
toolbar.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED)
Dhara Vamja
  • 554
  • 4
  • 17
1

Please use below code In first fragment before you do the fragment transaction.

rootView = inflater.inflate(R.layout.first_fragment, null, false);
rootView.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);

Hope this will help.

Sukhbir
  • 1,410
  • 14
  • 33
0

You can perform accessibility action in fragment onActivityCreated method like below

getParentActivity().getToolbar().
          performAccessibilityAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS, null);

Pravin Londhe
  • 865
  • 7
  • 14