1

I'm implementing a Maui app, and got stuck with the bug whereby the soft keyboard is not displayed when an Entry control gains focus. The entry control is focused, the cursor blinks, the keyboard remains hidden.
(For the other way around, hiding the keyboard, I am aware of the disable/enable hack.)
I tried many things, the next one is using platform interop to force-show it.
I found some suggestion to use Context.getSystemService(Context.InputMethodManager) as InputMethodManager, but getSystemService is not avaialable on the Android.Content.Context object provided by Maui.

So to get me started, can someone help with an example on how to use getSystemService from Maui to get a reference to an InputMethodManager?

XAML

<ContentPage.Content>
<ScrollView>
    <StackLayout x:Name="MainLayout"
        Padding="30,100">
        <Label Text="Password" />
        <Frame CornerRadius="15" Padding="0">
            <Entry x:Name="PinEntry" 
                Text="{Binding Pin}"
                FontSize="20"
                HorizontalTextAlignment="Center"
                Loaded="PinEntry_Loaded"
                Keyboard="Telephone"/>
        </Frame>
        <Button x:Name="LoginButton"
            Text="Log in"
            FontSize="20"
            Clicked="LoginButton_Clicked"
            Padding="12"
            Margin="0,30" />
    </StackLayout>
</ScrollView>
</ContentPage.Content>

Codebehind

private async void PinEntry_Loaded(object sender, EventArgs e)
{
    PinEntry.Focus();
}

Also tried

private async void PinEntry_Loaded(object sender, EventArgs e)
{
    PinEntry.Focus();
    var _ = SoftKeyboardHelper.Show();
}

where SoftkeyboardHelper complete implementation is:

#if ANDROID
using Android.Content;
using Android.Views;
using Android.Views.InputMethods;
using Android.Runtime;
using Android.OS;
#endif

namespace Waiter.Maui.Platforms.Android
{
    public partial class SoftKeyboardHelper
    {
        public static async Task Show()
        {
#if ANDROID
            await Task.Run(async () =>
            {
                await Task.Delay(5000);
                var imm = (InputMethodManager)MauiApplication.Current
                    .GetSystemService(Context.InputMethodService);
                if (imm != null)
                {
                    var activity = Microsoft.Maui.ApplicationModel
                        .Platform.CurrentActivity;
                    IBinder wToken = activity.CurrentFocus?.WindowToken;
                    imm.ShowSoftInputFromInputMethod(wToken, 0);
                }
            });
#else
            // Do nothing on other platforms
#endif
        }
    }
}
balintn
  • 988
  • 1
  • 9
  • 19
  • Not an answer to question, but if you are testing on an emulator, test on an actual device. Same symptom? My thinking is that emulators run on computers which have physical keyboards; this could confuse soft keyboard into not showing. – ToolmakerSteve Feb 20 '23 at 01:01
  • 1
    Ah, good call but no luck. Just tested on a physical device and it behaves the same. The Entry control has focus, the txt cursor is blinking there but the soft keyboard is not deisplayed :-( The device is OnePlus6, Android 11. I'll try on some others. – balintn Feb 22 '23 at 21:08
  • Show the layouts that Entry is nested in. Something like ScrollView > StackLayout > ListView > Entry, or whatever. Do you have a link to a description of this bug? – ToolmakerSteve Feb 22 '23 at 23:40
  • [Context.GetSystemService](https://learn.microsoft.com/en-us/dotnet/api/android.content.context.getsystemservice?source=recommendations&view=xamarin-android-sdk-13). Are you sure you are correctly asking for it? Maybe you got Application class from a different namespace. Inside Android code, try `global::Android.App.Application.Context.GetSystemService`, from https://stackoverflow.com/a/60784646/199364. – ToolmakerSteve Feb 22 '23 at 23:43
  • Thank you for your reply ToolmakerSteve, added XAML and codebehind to demonstrate the issue. – balintn Mar 12 '23 at 18:39
  • Just tested my sample, and realised it was not in line with my observations. Updating the example so that the entry is focused on the entry's Loaded event. Now it behaves as originally described: text cursor blinking in the entry, soft keyboard not displayed. – balintn Mar 12 '23 at 21:22
  • Tried on physical device, tried Task.Run(() => ...) delaying .Focus() in PinEntry_Loaded, but it behaves the same. Checked result value of the .Focus call, it returns true, meaning a successful focus attempt. Will look into your other suggestion around using GetSystemService. – balintn Mar 12 '23 at 21:37
  • 1
    Managed to come up with code using InputMethodManager, but I can't make it display the soft keyboard ither. Tested on physical device and Pixel5 API 30 emulator. Updating original question with new code. – balintn Mar 12 '23 at 23:30
  • Maybe Android OS doesn't correctly focus while inside Loaded method. Wrap contents of the Loaded method(s) in `Dispatcher.Dispatch(async () => { await Task.Delay(100); ...method-code-here... });`. This queues the code to run after Loaded method returns. The delay is optional; if doesn't work, as an experiment, try making it 1000. – ToolmakerSteve Mar 13 '23 at 20:53
  • Nah. The event handler looks like this now, but cursor blinks, keybord remains hidden. private void PinEntry_Loaded(object sender, EventArgs e) { Dispatcher.Dispatch(async () => { await Task.Delay(1000); PinEntry.Focus(); var _ = SoftKeyboardHelper.Show(); }); } – balintn Mar 13 '23 at 22:00
  • Ha! I have a working example! Created default Maui app from VS (2022, v17.4.4, Maui 7). Removed most of MainScreen components, added a label and an entry (named MyEntry), kept the button. Added super simple MyEntry Loaded event handler private void MyEntry_Loaded(object s, EventArgs e) { MyEntry.Focus(); } and it works as expected on the pixel_5_-_api30 emulator: cursor blinks in entry and keyboard opens up! I'll try to work the gap between my page and this example to fix it. Thx ToolmakerSteve for all your attention! – balintn Mar 13 '23 at 22:23
  • Drumroll... And the winner is: TargetFramework net7.0-android. The sample app initially targets net6.0-android, and works correctly (focusing an entry field opens up the soft keyboard). Switching to net7.0-android, restarting VS, rebuilding solution and executing results in soft keyboard not showing. I guess I'll have to report it as a bug with the Maui team. – balintn Mar 13 '23 at 23:11
  • Reported here: https://github.com/dotnet/maui/issues/13909 – balintn Mar 14 '23 at 12:25

0 Answers0