0

I need to hide Softkeyboard when an entry is focused on Net MAUI on .net7 in Android in order to input data from a barcode Scanner.

So far I tried to disable/enable entry when focused, but the behavior is not correct.

I also tried a KeyBoardHelper that I found with the same behavior (not showing cursor or when entry is focused I changed backgroundcolor and with this helper does not change).

public static partial class KeyboardHelper
    {
        public static void HideKeyboard()
        {
            var context = Platform.AppContext;
            var inputMethodManager = context.GetSystemService(Context.InputMethodService) as InputMethodManager;
            if (inputMethodManager != null)
            {
                var activity = Platform.CurrentActivity;
                var token = activity.CurrentFocus?.WindowToken;
                inputMethodManager.HideSoftInputFromWindow(token, HideSoftInputFlags.None);
                activity.Window.DecorView.ClearFocus();
            }
        }
    }

Is there other way to prevent to show keyboard when an entry is focused?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65

3 Answers3

1
Put this in your \Platforms\Android\MainActivity.cs:
-------------------------------------------------------

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        KeyboardService.lastContext = this;
        ....
    }
       
    public void HideKeyboard()
    {
        try
        {
            InputMethodManager imm = (InputMethodManager)GetSystemService(InputMethodService);
            imm.HideSoftInputFromWindow(CurrentFocus.WindowToken, HideSoftInputFlags.None);
        }
        catch
        { }
    }

    public void ShowKeyboard(Android.Views.View view)
    {
        try
        {
            InputMethodManager imm = (InputMethodManager)GetSystemService(InputMethodService);
            view.RequestFocus();
            imm.ShowSoftInput(view, 0);
            imm.ToggleSoftInput(ShowFlags.Forced, HideSoftInputFlags.ImplicitOnly);
        }
        catch (Exception e) { }
    }


Create a new file \Platforms\Android\Services\KeyboardService.cs
-----------------------------------------------------------------

using Android.Content;
using Android.OS;
using Android.Views;

internal static partial class KeyboardService
{
    public static Context lastContext;
    public static bool KeyboardIsVisible = false;

    public static void HideKeyboard()
    {
        ((MainActivity)lastContext).HideKeyboard();
        KeyboardIsVisible = false;
    }

    public static void ShowKeyboard(Entry entry)
    {
        ((MainActivity)lastContext).ShowKeyboard((Android.Views.View)entry.Handler.PlatformView);
        KeyboardIsVisible = true;
    }
}




create the empty counterpart file on the "non android" side (for each platform used) f.e.: yourProject\Services\KeyboardService.cs
----------------------------------------------------------------------------------------------------------------------------------

internal static partial class KeyboardService
{
}



Call the android functions anywhere you need to:
--------------------------------------------------------
 KeyboardService.HideKeyboard();

 KeyboardService.ShowKeyboard(MyEntryFieldName);
  • is this suitable for net 7? i tried but get an error on ((MainActivity)lastContext).ShowKeyboard((Android.Views.View)entry.Handler.PlatformView); line about class Android.View not found – Diego Estraviz Silva Mar 30 '23 at 07:20
  • Newest VS2022 .NET7.0 MAUI. Using it with Zebra, Datalogic, Honeywell etc. barcode scanners to hide / show virtual keyboard on demand. Check the "using Android.Views" at top of .cs-Code and remove unneeded "usings". Often it helps to recompile the whole project to make something work. if only using \Platforms\Android there is documented howto really disable the other platforms. A few months ago that was not working (tizen remaining) per GUI project settings .... – Michael Haumann Mar 30 '23 at 07:58
  • The error then came from missing "partial class counterpart" from \Platforms\Tizen\Services\KeyboardService.cs – Michael Haumann Mar 30 '23 at 08:08
1

In an Entry handler:

handler.PlatformView.ShowSoftInputOnFocus = false;

Or customize a control on a Page or in MauiProgram using a property mapper:

Microsoft.Maui.Handlers.EntryHandler.Mapper
  .AppendToMapping("ShowSoftInputOnFocus", (handler, view) =>
{
  if (view is Entry entry)
  {
#if ANDROID
    handler.PlatformView.ShowSoftInputOnFocus = false;
#endif
  }
});
Benl
  • 2,777
  • 9
  • 13
  • In an entry handler worked well if the keyboard was hide, but when you focus a normal entry before and keyboard is shown, when focus on entry handle the keyboard keeps shown and does not hide – Diego Estraviz Silva Mar 29 '23 at 14:45
1

You can write the Handler in the MauiProgram.cs file to control the entry.

 Microsoft.Maui.Handlers.EntryHandler.Mapper.AppendToMapping("MyCustomization", (handler, view) =>
        {
#if ANDROID
            handler.PlatformView.ShowSoftInputOnFocus = false;
#endif
        });
Guangyu Bai - MSFT
  • 2,555
  • 1
  • 2
  • 8