maui: Soft Keyboard does not Pop Up when Entry View's Focus is set to True Programmatically

Description

When Focus is set to an Entry view programmatically (entryTest.Focus()), the cursor blinks in the edit field of the Entry view, but the soft keyboard does not pop up as it should until you physically touch the Entry view. In comparison to Xamarin Forms where the keyboard pops up when executing entryTest.Focus().

Also, entryTest.Focus() is ignored when placed in the OnAppearing Event.

Video demo of Maui App:

https://user-images.githubusercontent.com/101732760/162827580-c62e0530-9fa1-4f90-b744-90c37caeec57.mp4

Video demo of Xamarin Forms version:

https://user-images.githubusercontent.com/101732760/162827635-9904fcb9-7276-4eba-8d90-ac4c2c7019d9.mp4

XAML: image

Code Behind: image

Steps to Reproduce

  1. Create a new blank Maui app
  2. Add and Entry View and name it entryTest
  3. Add 2 Buttons and set Text to “Set Focus” and “Set Unfocus”
  4. In the Code behind for the 1st button add code entryTest.Focus()
  5. In the Code behind for the 2nd button add code enryTest.Unfocus()
  6. Run the App
  7. Click the Set Focus button.
  8. Notice the Entry field cursor blinks
  9. Notice the keyboard does not pop up
  10. Tap the Entry field.
  11. Notice the keyboard pops up.

Version with bug

Preview 14 (current)

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 11

Did you find any workaround?

Workaround

I added a mapper here that you can copy to add this behavior for android.

One thing to note is that if your intention is to open the keyboard you should use KeyboardManager.ShowKeyboard not Focus

Using Focus to influence the Keyboard has always felt like a hack to me in general.

Relevant log output

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 16
  • Comments: 26 (3 by maintainers)

Commits related to this issue

Most upvoted comments

to this day it still doesn’t work

Surprised to see ! It isn’t working till today.

I added a mapper here that you can copy to add this behavior for android.

One thing to note is that if your intention is to open the keyboard you should use KeyboardManager.ShowKeyboard not Focus

Using Focus to influence the Keyboard has always felt like a hack to me in general.

Quick Solution : Make Entry , Editor or Any thirdparty controls IsEnabled = false then Entry.UnFocus() to hide the key board and Again don’t forget to make IsEnabled property to true as soon as the task is completed. 😃

This definitely needs a fix

Here’s some code you can use to interact with the keyboard for the time being

https://github.com/PureWeen/ShanedlerSamples/tree/main/ShanedlerSamples/Keyboard

If you call “ShowKeyboard” there’s no need to call “Focus” when using this code.

Still not working in GA. Is there a method, to show/hide the ENTRY keyboard manually?

I’ve encountered a different problem. Keyboard is not hiding when unfocus entry. I made a wordaround in handler:

protected override void ConnectHandler(AppCompatEditText platformView)
{
	base.ConnectHandler(platformView);

	if (VirtualView is Entry entryControl)
		entryControl.ObserveUnfocused().Subscribe(UnfocusedSubscription);
}

private void UnfocusedSubscription(FocusEventArgs args)
{
	if (!args.IsFocused)
	{
		InputMethodManager inputMethodManager = (InputMethodManager)global::Android.App.Application.Context.GetSystemService(global::Android.Content.Context.InputMethodService);
		inputMethodManager.HideSoftInputFromWindow(PlatformView.WindowToken, HideSoftInputFlags.None);
	}
}

I use own observable method, but you can use events.

I’ve encountered the same problem. But when I use “Entry” inside Grid instead of StackLayout, it works just fine. Entry and StackLayout don’t get along yet.