Mauricio Sartori
Mauricio Sartori

Reputation: 2621

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?

Upvotes: 2

Views: 3224

Answers (2)

Itay Feldman
Itay Feldman

Reputation: 896

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.

Upvotes: 4

MobA11y
MobA11y

Reputation: 18860

First, it's important to note that Fragments function no different than layouts within normal activities. Fragments are just a convenient way of structuring the code. What Android/the Accessibility APIs see if you code up your application as a series of fragments, or a bunch of layouts within a single activity is identical. That being said, I believe what you're looking for is the following:

What you need to do is move accessibility focus.

myCustomView.requestFocus();

is moving what I consider input focus. This will have no effect on TalkBack, and is in fact all but completely meaningless, unless you are using keyboard navigation, or we are talking about an EditText box. What you want to do is move Accessibility/TalkBack focus. This can be accomplished with the following line of code:

myCustomView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);

NOTE: Please keep in mind WCag 2.0 criteria, especially when it comes to moving focus around on TalkBack users automatically. This can become very confusing for non-sighted users!

Upvotes: 0

Related Questions