Skip to content

[Android] MAUI 8.0.3 -> 8.0.6 regression: custom handler with key listener no longer works #21109

@ysbakker

Description

@ysbakker

Description

Some context

Localization of numeric keyboards on Android can be pretty confusing, so we wrote a workaround (loosely based on this solution) that allows the user to both use a period and a comma as a decimal separator. We added some logic to always translate the separator to the expected one, and an Android handler which adds a key listener to the Entry view that allows the user to enter both a '.' and a ','.

Handler:

public partial class DecimalEntryHandler : EntryHandler
{
    public DecimalEntryHandler()
    {
        Mapper.AppendToMapping(nameof(IEntry.Keyboard), SetKeyboard);
    }
}

Android handler:

public partial class DecimalEntryHandler
{
    private static void SetKeyboard(IEntryHandler handler, IEntry entry)
    {
        handler.PlatformView.KeyListener = new NumericKeyListener(handler.PlatformView.InputType);
    }
}

NumericKeyListener:

public class NumericKeyListener : NumberKeyListener
{
    public override InputTypes InputType { get; }
    protected override char[] GetAcceptedChars() => "0123456789-,.".ToCharArray();

    public NumericKeyListener(InputTypes inputType)
    {
        InputType = inputType;
    }

    public override bool OnKeyDown(global::Android.Views.View? view, IEditable? content, Keycode keyCode, KeyEvent? e)
    {
        Application.Current.MainPage.DisplayAlert("OnKeyDown", string.Empty, "OK");
        return base.OnKeyDown(view, content, keyCode, e);
    }
}

The problem

The key listener no longer works in MAUI 8.0.6. The handler is registered, and the key listener is also added to the view. The breakpoint is hit in the NumericKeyListener constructor. But it no longer gets used. This results in the user only being able to enter their localized decimal separator.

I made a reproducible example here: https://github.com/ysbakker/MauiAndroidKeyboardIssueReproduction

You can test this by changing the SDK in the global.json to either 8.0.100 (working) or 8.0.201 (not working) and running dotnet workload restore if you do not have that MAUI version. A dialog should show up whenever you press a button, which doesn't happen on the latter SDK version.

What could be causing this?

Steps to Reproduce

  1. Run the attached project in MAUI 8.0.6
  2. Note that the dialog does not show up when pressing a key
  3. Note that it does show up in MAUI 8.0.3

Link to public reproduction project repository

https://github.com/ysbakker/MauiAndroidKeyboardIssueReproduction

Version with bug

8.0.6 SR1

Is this a regression from previous behavior?

Yes, this used to work in .NET MAUI

Last version that worked well

8.0.3 GA

Affected platforms

Android

Affected platform versions

No response

Did you find any workaround?

No. But do let me know if you have a different solution for the numeric input.

Relevant log output

No response

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions