Search code examples
c#androidxamarinaccessibilitytalkback

How to set my SimplestExploreByTouchHelper as a AccessibilityDelegate in ViewCompat in Xamarin?


I am rather new to the TalkBack functionality, and I need help. I have a Xamarin app, and I need it to work the DPAD keyevents (up, down, left, right) when the TalkBack function is active.

Of course, this is a rather new option and there is no information, none, nada, nothing about this on non native components which my Xamarin app uses, and I had to convert code from java to c#.

Following this site: https://developer.android.com/training/tv/accessibility/non-native-best-practices

I have done what I could, but I have to interpret the last function to c#, and here is the problem.

Java code:

private void init(@Nullable AttributeSet attrs) {
    ...
    ViewCompat.setAccessibilityDelegate(this,
    new SimplestExploreByTouchHelper(this));
}

I assume that I have to call this function in my MainActivity.cs file inside the OnCreate function. But, since that is Java, I have to convert it to C#:

C#

 protected override void OnCreate(Bundle savedInstanceState)
 {
     ...
     SimplestCustomView scv = new SimplestCustomView(this.ApplicationContext);
     var helper = new SimplestExploreByTouchHelper(scv);
     ViewCompat.SetAccessibilityDelegate(mGfxSurface, helper);
 }

And, as always, there is a problem I need help with. There is an error:

Argument 2: cannot convert from 'MyApp.SimplestExploreByTouchHelper' to 'AndroidX.Core.ViewAccessibilityDelegateCompat'

There is close to NO INFO on how to implement the TalkBack functions in the non native components using Xamarin.

Can you help me with the following questions?

  1. How to convert it?
  2. Should the function be called from the MainActivity.cs OnCreate function or not?

My goal is for my app to handle DPAD key down events instaed of them being consumed by the Accessibility Server while it handles the TalkBack. That problem is described here: https://developer.android.com/training/tv/accessibility/non-native-app


Solution

  • How to convert it?

        private void init(IAttributeSet attrs) {
            ViewCompat.SetAccessibilityDelegate(this, new SimplestExploreByTouchHelper(this));
        }
    

    Should the function be called from the MainActivity.cs OnCreate function or not?

    This function should be placed into the custom view class SimplestCustomView.

    And make sure that SimplestExploreByTouchHelper inherit from ExploreByTouchHelper in your code .