Search code examples
androidtalkbackaccessibility-api

How to activate Talkback inside of an fragment's views?


I'm currently adding accessibility as a new feature inside my app. My goal is that the user would be navigating it using the TalkBack service integrated by Android.

Everything is working well, since I'm setting content description on the elements that are inside my activity layout i.e.

        <View
          style="@style/custom.style"
          android:contentDescription="@string/my_string_value"/>

This way, every time that my activity is displayed the TalkBack is reading the content description value.

I haven't had the same success using just one activity which is pushing several fragments on it. So if I try to set a content description on any element inside the fragment layout this is not gonna be read (automatically) till it detects a touch event (I'm expecting to the TalkBack does it automatically, just like the views that are in the activity layout)

In order to get a result as the one that I expect I this this inside the fragment class:

public abstract class myFragment extends Fragment  {
...
  @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,   Bundle savedInstanceState) {
       ...
       myCustomView = (LinearLayout) rootView.findViewById(R.id.duende);
       myCustomView.requestFocus();
  }
}

This haven't had success so far, same thing setting the accessibility as a content changed.

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

Does anyone had faced a similar issue?


Solution

  • Not sure your still looking for the solution but for future seekers :) -

    Many times the focus request works only once called from within a post/postdelay function.

    Example -

    myCustomView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    myCustomView.setFocusable(true);
                    myCustomView.requestFocus();
                    myCustomView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
                }
            },1000);
    

    While working with fragments, I like calling both the focuses, input focus, like called by the guy in the previous answer, and the accessibility focus because while implementing accessibility onto fragments you can get pass some irritating problems. hence this always does the job for me.