Skip to content

[BUG] Touchbehavior Keyboard Access Click not working on Android #2443

Closed

Description

@plewm

Is there an existing issue for this?

  • I have searched the existing issues

Did you read the "Reporting a bug" section on Contributing file?

Current Behavior

When you use an App on Android via Keyboard access/navigation and you try to hit a button via space bar for example, the animation is executed, but the click event is not raised or executed

Expected Behavior

You would expect the Click to be executed normally, like it does on iOS

Steps To Reproduce

  1. Just add the Touchbehavior to any component
  2. try using the App with Keyboard
  3. navigate to that component
  4. press space bar to initiate click

In the Example App:

  1. Navigate via Tab on keyboard to the Border with TouchBehavior
  2. Press Space bar to Click -> nothing happens
  3. Touch normally -> works
  4. activate TalkBack
  5. Navigate again by Tab on Keyboard
  6. Press Space bar -> works

Link to public reproduction project repository

https://github.com/plewm/TouchbehaviorExample

Environment

- .NET MAUI CommunityToolkit: 9.1.0
- OS: macOS 15.1 (24B83)
- .NET MAUI: 8.0.402.1

Anything else?

From my debugging I found the issue to be the accessibilityManager and bool IsAccessibilityMode. it seems the accessibilityManager is null and accessibility mode is just false if the App is only used with keyboard. It will only be set if TalkBack is activated on the device. The Keyboard actions than also work, when TalkBack is activated as well. But the App should be usable with only keyboard without TalkBack as well. The Problem is, that the bool IsAccessibilityMode is checked in the OnClick method and is not reaching the HandleTouchEnded if AccessibilityMode is false.:

`void UpdateClickHandler()
{
if (view is null || !view.IsAlive())
{
return;
}

	view.Click -= OnClick;
	if (IsAccessibilityMode || (IsEnabled && (Element?.IsEnabled ?? false)))
	{
		view.Click += OnClick;
		return;
	}
}

void OnClick(object? sender, EventArgs args)
{
if (!IsEnabled)
{
return;
}

	if (!IsAccessibilityMode)
	{
		return;
	}

	IsCanceled = false;
	HandleTouchEnded(TouchStatus.Completed);
}`

Furthermore that implementation feels not as Android intended cause the documentation on Android side says not to do different stuff based on the AccessibilityManger because it results in inconsistent behavior.

isEnabled
Added in API level 4
public boolean isEnabled ()
Returns if the accessibility in the system is enabled.
Note: This query is used for sending AccessibilityEvents, since events are only needed if accessibility is on. Avoid changing UI or app behavior based on the state of accessibility. While well-intentioned, doing this creates brittle, less well-maintained code that works for some users but not others. Shared code leads to more equitable experiences and less technical debt.

https://developer.android.com/reference/android/view/accessibility/AccessibilityManager

Activity

  1. jfversluis commented on Jan 16, 2025

    @jfversluis
    Member

    I see you mention .NET MAUI on .NET 8, are you able to test with .NET 9? And the latest Toolkit release? Does that change anything? Just to make sure we're looking at the latest and greatest?

    It does seem that you already dug in quite a bit. Maybe you're able to propose a change for this through a PR?

  2. plewm commented on Jan 22, 2025

    @plewm
    ContributorAuthor

    I just retested it with .NET9 and latest Maui release 9.0.100 and CommunityToolkit 11.0.0. Currently I can only test Android, but now the Touchbehavior is not working at all anymore.

    Additional testing: 9.1.1 still works, 10.0.0 and 11.0.0 do not work

  3. ne0rrmatrix commented on Jan 26, 2025

    @ne0rrmatrix
    Member

    Be sure to assign the BindingContext after switching to dotnet 9.x The toolkit no longer set binding context. It is a breaking change. More information is available in the release notes: https://github.com/CommunityToolkit/Maui/releases/tag/10.0.0

    Additional information available on StackOverflow: https://stackoverflow.com/a/79099574/5953643

  4. plewm commented on Jan 28, 2025

    @plewm
    ContributorAuthor

    Yep, that helps. The standard functionality with that manual Binding works.

    The original issue of the Android Keyboard usage persists. even with .NET 9 and newest Toolkit version

  5. plewm commented on Feb 5, 2025

    @plewm
    ContributorAuthor

    I would like to create a PR for a change, but I have no rights to create a branch or something.

    My proposal for a fix would be to separate the event of the KeyBoard action by reacting to the KeyPress event. It results so that the click event is not triggered by the spacebar anymore and only raises the KeyPress event:

    protected override void OnAttachedTo(VisualElement bindable, AView platformView)
    	{
    		base.OnAttachedTo(bindable, platformView);
    		Element = bindable;
    		view = platformView;
    		viewGroup = platformView.GetParentOfType<ViewGroup>();
    
    		platformView.Touch += OnTouch;
    		platformView.KeyPress += OnKeyPress;
    		UpdateClickHandler();
    		accessibilityManager = platformView.Context?.GetSystemService(Context.AccessibilityService) as AccessibilityManager;
    
    		if (accessibilityManager is not null)
    		{
    			accessibilityListener = new AccessibilityListener(this);
    			accessibilityManager.AddAccessibilityStateChangeListener(accessibilityListener);
    			accessibilityManager.AddTouchExplorationStateChangeListener(accessibilityListener);
    		}
    
    		if (!IsAndroidVersionAtLeast((int)BuildVersionCodes.Lollipop))
    		{
    			return;
    		}
    
    		platformView.Clickable = true;
    		platformView.LongClickable = true;
    	}
    
    
    protected override void OnDetachedFrom(VisualElement bindable, AView platformView)
    	{
    		base.OnDetachedFrom(bindable, platformView);
    		view = platformView;
    
    		try
    		{
    			if (accessibilityManager is not null && accessibilityListener is not null)
    			{
    				accessibilityManager.RemoveAccessibilityStateChangeListener(accessibilityListener);
    				accessibilityManager.RemoveTouchExplorationStateChangeListener(accessibilityListener);
    				accessibilityListener.Dispose();
    				accessibilityManager = null;
    				accessibilityListener = null;
    			}
    
    			if (view is not null)
    			{
    				view.Touch -= OnTouch;
    				view.Click -= OnClick;
    				view.KeyPress -= OnKeyPress;
    			}
    
    			Element = null;
    		}
    		catch (ObjectDisposedException)
    		{
    			// Suppress exception
    			Trace.WriteLine("TouchBehavior is already disposed.");
    		}
    
    		isHoverSupported = false;
    	}
    
    void OnKeyPress(object? sender, AView.KeyEventArgs e)
    {
    		if (e.KeyCode == Keycode.Space && e.Event?.Action == KeyEventActions.Up)
    		{
    			IsCanceled = false;
    			HandleTouchEnded(TouchStatus.Completed);
    		}
    	}
    
  6. TheCodeTraveler commented on Feb 5, 2025

    @TheCodeTraveler
    Collaborator

    Thanks @plewm. You must first fork the repository before you can submit a pull request. Here's a walkthrough: https://opensource.com/article/19/7/create-pull-request-github

  7. plewm commented on Feb 6, 2025

    @plewm
    ContributorAuthor

    you can look at:

    #2497

    do I have to add you guys or someone specific as reviewer somehow?

  8. locked and limited conversation to collaborators on Mar 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/behaviorsIssue/Discussion/PR that has to do with BehaviorsbugSomething isn't workingunverified

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions